Error while printing Nodes in C? - c

I am trying to print what I have in nodes but it says the following error
main.c: In function 'main':
main.c:83:37: error: request for member 'emails' in something not a structure or union
printf("%s\n", tmpNodesUnique[l].emails);
^
I get the above error after I run the following code. What am I doing wrong here?
Node *tmpNodesUnique[nodesCount];
int uniqueFound = 0;
tmpNodesUnique[0] = &tmpNodes[0];
for (k=1; k<10; k++){
if (strcmp(tmpNodesUnique[uniqueFound]->emails, tmpNodes[k].emails) != 0){
tmpNodesUnique[++uniqueFound] = &tmpNodes[k];
}
}
for (k=0; k<=uniqueFound; k++){
tmpNodesUnique[k]->emails;
}
for(l = 0; l <= nodesCount; l++){
printf("%s\n", tmpNodesUnique[l]->emails);
}

Probably your struct Node is defined as
typedef struct {
char *emails;
} Node;
This means if you want to print the member emails you will have to use . operator and not the -> operator
for(l = 0; l <= nodesCount; l++){
printf("%s\n", tmpNodesUnique[l].emails);
}
And as pointed out in the comments the following line does nothing, it is an incomplete statement
for (k=0; k<=uniqueFound; k++){
tmpNodesUnique[k]->emails;
}
Additionally make sure you really want k<= instead of k< it looks suspicious as well

It looks like you're not compiling the code you expect.
The error uses a . member operator for emails. While you have a -> pointer operator in the code you show. So the code is different (and the error is related to the dot as it's stating that it expects a structure or union member while you've clearly declared a pointer).

Related

C Illegal instruction: 4 upon assignment to a global variable

I am working on a compiler project using lex and yacc, and have declared a global "localVarCount" initialized to zero. when this function is called by the generated yacc code, and localVarCount is assigned to in any way, it throws the "Illegal instruction: 4" error during runtime. The offending line is shown below. I do similar incrementing and assignment to that variable in other functions with no problems.
void insertArrayScopedName(char * name, int arraySize) {
int VarCount = localVarCount;
enterName(localTable, name);
if(arraySize < 1) {
writeIndicator(getCurrentColumnNum());
writeMessage("Cannot declare an array with a size smaller than 1");
errorFlag1++;
}
for (int i = 0; i < arraySize; i++){
setCurrentAttr(localTable, (void *) VarCount);
VarCount++;
}
localVarCount = VarCount; //Offending line
return;
}

Why can't I declare more than one variable in a for loop's initial statement?

When using a with one variable, we declare it to 0 as i=0 like here
but when we use two variables as if I add n = strlen to make the code more efficient then i=0 is not declared but a comma is used and n=strlen(s) is declared. Why can't we use 'i=0;' here as done in the previous code?
Edit: The cs50.h is part of the sandbox cs50 which is made by harvard.
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output: ");
for (int i = 0; i < strlen(s); i++)
{
printf("%c\n", s[i]);
}
}
#include <cs50.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
string s = get_string("Input: ");
printf("Output:\n");
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c\n", s[i]);
}
}
From C standard ISO/IEC 9899:TC3 / ISO/IEC 9899/C11, the grammar description of the for statement, §(6.8.5) iteration-statement, have 2 different forms:
for ( expression_opt ; expression_opt ; expression_opt ) statement
for (declaration expression_opt ; expression_opt ) statement
Note that in the first form we have an expression in the first position, although it is optional as all 3 fields, and all the 3 parts of the statement form are separated by a ; semicolon used as a delimiter.
In the second form we can see that the first element in parenthesis is a declaration instead of an expression, and we can note also that there is no semicolon delimiting it. This is because the declaration itself is always terminated by a semicolon.
Paragraph (6.7) declaration describe grammar for a declaration:
declaration-specifiers init-declarator-list_opt ;
As you can see a declaration is always terminated by a semicolon.
A declaration can assume of course the form of multiple declarations initialized as in your:
for (int i = 0, n = strlen(s); i < n; i++)
{
printf("%c\n", s[i]);
}
So you are using the second form where the semicolon delimits the declaration (not the first part of the for).
Using multiple semicolon is simply a grammar error.
Not sure if I understand the question (although I like it). Both your code snippets work, don't they? Note that we don't know what is in cs50.h, but I tried compiling this, and it worked (both compiling and running).
#include <stdio.h>
#include <string.h>
int main(void) {
char *s = "Hello world!";
printf("Output:\n");
for (int i = 0, n = strlen(s); i < n; i++) {
printf("%c\n", s[i]);
}
}
Two things might be relevant here;
- how for works and
- how variable declaration/initialization works.
You can think about for like this:
for(AAA; BBB; CCC) DDD;
is the same as
{ // Important!
AAA;
while(BBB) {
{
DDD;
}
CCC;
}
} // Important!
The // Important! braces are important because the for introduces a new scope, i.e. i and n won't accessible outside/after the for loop.
The other thing is declaration/initialization. So the
int i = 0, n = strlen(s);
is an initialization of two variables: i, n. I'm not 100% sure about the proper vocabulary and rules (you can consult the standard), but the idea is that a declaration looks like:
TYPE VAR1, VAR2, ..., VARn
where the VARx is a variable name declared, or an "assignment" in which case it is an initialization.
UPDATE/PART2:
Usually how I would do this is something like:
const int len = strlen(s);
// Good practice to declare const what every you know wont change
for(int i = 0; i < len; i++) {
// whatever
}
But, what if the confusing coma/semicolon could be made consistent and since the semicolon is a must let's try to make everything a semicolon, I've tried this:
for ({int i = 0; int n = strlen(s); }; i < n; i++) {
// what ever
}
This did not compile, but it also didn't make sense, since if this would have "worked" (in the sense I thought I could but actually couldn't), i and n would be declared in the small block and it wouldn't be accessible anywhere else, i.e. in i < n would not be accessible. So to make them accessible we could try this:
int i, n;
for ({i = 0; n = strlen(s); }; i < n; i++) {
printf("%c\n", s[i]);
}
Now this should have worked if the for-while equivalency stated above would be 100% true, but it's not since apparently the the AAA has to be a single statement (usually a declaration) and it can't be a block i.e. {...}. Exact compiler error:
cc hola.c -o hola
hola.c: In function ‘main’:
hola.c:8:8: error: expected expression before ‘{’ token
8 | for ({
| ^
make: *** [<builtin>: hola] Error 1
but as you can see it is already very ugly and all... so yes, you need to use , to separate the declarations/initializations and a ; to terminate it.

Obviously declared variable is considered undeclared [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have the following code* inside an if else {} code block but the GCC compiler throws me this error:
hw2b.c: In function `printSymbolK':
hw2b.c:26: parse error before `int'
hw2b.c:27: `w' undeclared (first use in this function)
hw2b.c:27: (Each undeclared identifier is reported only once
hw2b.c:27: for each function it appears in.)
*
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
Can someone tell me what i'm missing here? The w variable is declared and initialized inside the if else block but it says it isn't. Any ideas?
EDIT: You were right, i only copied a small piece of my program. Here is the full code:
#include <stdio.h>
/*
* The printSymbolK function accepts a
* natural number (N > 3) and prints the
* symbol K to the console according to certain
* specifications.
*/
void printSymbolK(int N) {
int i = 0,k=0;
int katoAkeraioMeros = N/2; // to apotelesma tou pilikou tha strogkilopoihtei pros ta kato dinontas mas to kato akeraio meros tou N/2
char star = '*';
for(i = 0;i < N; i++) {
if( i < katoAkeraioMeros) { // katoAkeraioMeros - i+1 = posa tha einai apo pano
int res = katoAkeraioMeros - 1; // poses fores tha trexei i epanalipsi
int l = 0;
printf("%c",star);
for(l = i;l < res;l++) {
printf(" ");
}
printf("%c\n",star);
} else if(i+1 == katoAkeraioMeros) {
printf("%c%c\n",star,star);
} else if(i > katoAkeraioMeros) { // auxise to space kata 1 gia kathe i apo edo kai pera
printf("%c",star);
int w = 0;
for(w = 4; w < i; w++) {
printf(" ");
}
printf("%c\n",star);
} else {
printf("%c %c\n",star,star);
}
}
}
int main() {
// Stelios Papamichail 4020
int n = 0;
do {
scanf("%d",&n);
printf("\n\n");
} while(n <= 3);
printSymbolK(n);
return 0;
}
the first error says it all:
hw2b.c:26: parse error before `int'
So something triggered a syntax error before the int token.
you shouldn't worry about further errors. at this point the parser is confused and thinks you're using w whereas you're trying to declare it (compilers don't stop at the first error so you can fix several errors at once, but sometimes that backfires, and sometimes they stop at some error, and when you fix it, you get more errors... compiler does its best with invalid code)
Just fix the first error, and retry.
Without seeing the full code, I would guess your using an older C compiler (C89) and you have code that is neither variable definition nor declarations above 'int w = 0;'.

struct member is not found

I have the following struct:
typedef struct vertex_tag{
int visited = 0;
int weight = FLT_MAX;
int prev;
}vertex_t;
It has the three members as indicated above.
I malloc the vertex like this:
vertex_t * vertex[G->vertices];
for(i=0; i < G->vertices; i++)
{
vertex[i] = (vertex_t*)malloc(sizeof(vertex_t));
}
So I create a matrix from the struct. I then call them throughout the function I created like this:
vertex[i]->visited
vertex[i]->weight
vertex[i]->prev
I keep getting the following error:
error: ‘vertex_t’ has no member named ‘visited’
error: ‘vertex_t’ has no member named ‘weight’
error: ‘vertex_t’ has no member named ‘prev’
Can anyone help me understand why I cant do this?
Okay so I can do it after the for loop in which I malloced it?
You'd do it better in the loop.
vertex_t *vertex[G->vertices];
for (i = 0; i < G->vertices; i++)
{
vertex[i] = malloc(sizeof(vertex_t));
vertex[i]->visited = 0;
vertex[i]->weight = FLT_MAX;
}
or according to Zeta's suggestion:
vertex_t vertex[G->vertices];
for (i = 0; i < G->vertices; i++)
{
vertex[i].visited = 0;
vertex[i].weight = FLT_MAX;
}

Incompatible types in assignment - C

I'm trying to sort an array of strings, but my compiler keeps saying I have imcompatible types in my assignment.
Below is the code in question.
for(i = 0; i < 499; i++) {
max = 0;
for(j = 1; j < 500; j++) {
if(strncmp(user_id[max], user_id[j], 9) > 0) {
printf("max = %s, j = %s\n", user_id[max], user_id[j]);
temp = user_id[j];
user_id[j] = user_id[max];
user_id[max] = temp;
}
}
}
The following two lines throw the error:
user_id[j] = user_id[max];
user_id[max] = temp;
Why is it that I am receiving this error?
EDIT:
Sorry, I forgot to include this before.
char user_id[500][9];
char* temp;
i j and max are int.
rover-208-149:prog3 kubiej21$ gcc --ansi --pedantic -o prog3 prog3.c
prog3.c: In function ‘main’:
prog3.c:46: error: incompatible types in assignment
prog3.c:47: error: incompatible types in assignment
Arrays are not assignable in C. So the following is not valid:
char user_id[500][9];
user_id[23] = user_id[42]; // Error: trying to assign array
I'm not sure what you're trying to achieve, but perhaps memcpy is what you need?
memcpy(user_id[23], user_id[42], sizeof(user_id[23]));

Resources