2

I'm pretty new to C and am taking a class right now and having difficulty with a project. We have a structure in this project called a Symbol. We also have a structure in this project called a Node. Nodes have an array of Symbols. All I'm trying to do is set the property Node.symbols to another array of symbols like so:

   int max = 256;
   Symbol symbols[max];
   Node node1;
   node1.syms = symbols;

Error: incompatibles types when comparing 'struct Symbol[256]' from type 'Struct Symbol *'

I don't really understand why it thinks that symbols is a pointer, when it seems like an array of symbols to me. This might be really obvious but I haven't been able to fix it.

2
  • that means syms is declared as struct Symbol[256]. you need to copy over one by one. FYI, symbols is pointer too. if you don't understand this, it's better for you to review your textbook first. Commented Mar 4, 2015 at 22:18
  • symbols is not a pointer it is an array, but in C arrays are very close to pointers. can you post here how Node is defined? You can only do that assignment if node1.syms was a pointer. Commented Mar 4, 2015 at 22:23

3 Answers 3

3

You can't use the assignment operator = for arrays. You either have to use a loop:

for (int i = 0; i < max; ++i)
{
    node1.syms[i] = symbols[i];
}

or use memcpy:

memcpy(node1.syms, symbols, max * sizeof(symbols[0]));

BTW, max is a bad name to use for an array dimension (or anything else for that matter), since it clashes with the name of the max() function/macro.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I didn't realize that was a difference in C and the error was written in such a way that through me off. Looks like I still have a lot to learn in basic C, I appreciate the help
What about a preprocessor macro for MAX ?
1

You did not post the definition of Node and Symbol but I suppose that Node.syms is an array. In this case the problem is that in line node1.syms = symbols; you try to change the the sysm member of Node struct but it is a constant varible (every array is constant, you can not change its location).

You have to possibilities.

1.) You copy the elements from the destination array to syms array:

int numelem = 256;
for (int i = 0; i < numelem; i++)
{
    node1.syms[i] = symbols[i];
}

But it is very important to find out the perfect value of numelem variable. Namely it is nowhere checked that the node1.syms and symbols arrays have the same number of elements. If their lengths are not equal you can overrun on one of them addressing illegal memory areas. Another possible disadvantege is that in all the Node elements the syms array will have the same number of elements, you can change it only in compile time.

2.) You change the definition if Node structure:

struct Node {
   //...
   struct Symbol *syms;
   //...
}

It this case your code will be complied and arrays node1.syms will be pointed to the array symbol and you do not have to take care about the length of the arrays. But it is important that in this case you will have only one array in the memory! So, you have to take into account that arrays are normally allocated on stack which means that after exiting from a method where the array was created the memory space of array will be freed as well. If you create node1 also it the method (as in your example) it is fine. But if you create node1 as a global variable than symbols array has to be global as well. In general we can say that both variable have to create in the same scope.

Comments

0

symbols is a pointer. It is the head of the array symbols[256] and is of type Symbol*. Dereferencing by [] then adds sizeof(Symbol) for each increment, e.g. symbols[3] references the 4th element in the array at adress symbols+3*sizeof(Symbols)

Using [] of a pointer is safer than doing explicit pointer arithmetics

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.