C - Default value for pointer within a struct
I have the following very simplistic implementation for a single linked list in C:
typedef struct Node{
int data;
struct Node *next;
} node;
void printLL(node * start);
void addNode(node * head, node * add);
int main()
{
node first;
first.data = 99;
first.next = NULL;
node second;
second.data = 11;
addNode(&first, &second);
printLL(&first);
}
void addNode(node * head, node * add)
{
if(head->next)
{
addNode(head->next, add);
}
else
{
head->next = add;
}
}
void printLL(node * start)
{
printf("Data is %d\n", start->data);
if (开发者_StackOverflow社区start->next) {
printLL(start->next);
}
}
What I'm confused about is that if I don't explicitly set first.next = NULL, I get a EXE_BAD_ACCESS error. It happens when I try and check if that property is set or not to determine if the call should be made recursively. The other thing I don't understand is that if I set first.next to point to 'second', all functions work even though for 'second' I don't explicitly set it's next to NULL. So it seems as though there is some inconsistency in the default value for a pointer defined within a struct. Of course I'm probably doing something totally wrong but would be grateful if somebody could shed light on the matter. To boil everything down my questions are:
- What is the default value for a pointer defined within a struct?
- Assuming that there is no default, is there a simple way to set a default within the structs definition? (I looked through some C books and couldn't find an answer to this question)
So it seems as though there is some inconsistancy in the default value for a pointer defined within a struct.
That's exactly the problem - the compiler is does not set an automatic (local) variable to any default. So you may get something that results in EXE_BAD_ACCESS
, or you may get something that appears to work. It's luck of the draw (and is a bug even if it appears to work).
One of the drawbacks to C is that you are responsible for ensuring that your variables are initialized properly. A compiler may assist with warnings, not all compilers are as good as others in that regard.
is there a simple way to set a default within the structs definition?
There is not a way to do this in the definition of a struct. However, you can initialize a struct when a variable of that type is declared:
node first = { 99, NULL };
There's no default value for any pointer, it contains a garbage value. Unless you initialize all pointers with your own default value.
There is no default value for anything within a struct (unless it is created statically). And there is no way of providing one - if you want that, you should use C++, which provides constructors to do exactly that. If sticking with C, you need to initialise the struct explicitly.
If you want default pointer values, you can create a factory method that returns a new instance of the struct with the values set to your desired default values.
There is no default value. Each memory location will hold on to whatever value it has unless explicitly modified. Though this behavior itself is compiler dependent. The bottom line, you cant assume that it will contain a particular value.
To set a default value, a good practice is to explicitly assign values to the struct
attributes whenever you create an instance of it.
If you were using C++
, you would have constructors where you would/should set default values of the members.
On call to addNode(&first, &second);
, in the if
condition, you are doing head->next
which is what causing the problems. next
is not initialized by default and is causing EXE_BAD_ACCESS at run time.
But when you set it to NULL, the condition in if
statement fails and is getting you to the else
statement where you are actually assigning the pointer.
Automatic variables are not initialized at all in C, regardless whether they are struct
or not. You have to initialize them yourself.
But in contrast to what most answer here seem to imply there is a default for initialization of any data type, including pointers. This is done as if any component would be explicitly initialized by a plain value '0'. For your example
node first = { .data = 99 };
would have done the trick. next
is then guaranteed to be initialized properly. (If you don't have C99 omit the .data =
and hope that you never change the layout of your struct
:)
Observe also that this might be different from zeroing out a struct
with memset
or so. On some architectures the all 0 bit pattern might not be the correct initialization for a null pointer or a double
. So better stick to what the language itself provides as initialization mechanism.
Default values might depent on C compiler so it mostly better to provide them.
You have to init it after you created your struct instance like second.next = NULL
.
精彩评论