5

I have a struct that contains two other structs of the same type, and I want to initialize it to have both NULL to start. How do I do that? I've tried the below, but get compiler warnings with gcc.

#include <stdio.h>

typedef struct Segment {
    int microseconds;
} Segment;

typedef struct Pair {
    Segment mark;
    Segment space;
} Pair;

int main()
{
    Pair mark_and_space = { .mark = NULL, .space = NULL };

    return 0;
}

And the compiler warnings:

main.c:14:5: warning: initialization makes integer from pointer without a cast [enabled by default]                                                   
 Pair mark_and_space = { NULL,  NULL };                                                                                                           
 ^                                                                                                                                                
main.c:14:5: warning: (near initialization for 'mark_and_space.mark.microseconds') [enabled by default]                                               

main.c:14:5: warning: initialization makes integer from pointer without a cast [enabled by default]                                                   
main.c:14:5: warning: (near initialization for 'mark_and_space.space.microseconds') [enabled by default] 
1
  • 2
    change NULL to 0 because mark and space type is int Commented Aug 21, 2017 at 23:47

7 Answers 7

6

You can't. NULL is a pointer whose value is set to zero, but your mark and space properties are not pointer values. In your code as you have it, they will both be value types, allocated as part of your Pair struct.

Change the variables to Segment * instead of Segment, and you will be able to set them to NULL.

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

Comments

5

In order to intialise two sub-struct, which each contain a single int member to what gets closest to NULL, i.e. init all ints to "0" you can use this code:

#include <stdio.h>

typedef struct Segment {
    int microseconds;
} Segment;

typedef struct Pair {
    Segment mark;
    Segment space;
} Pair;

int main(void)
{
    Pair mark_and_space = { {0}, {0} };

    return 0;
}

If you want to init them to NULL, assuming that you think of pointers, which is the only thing which can cleanly be intialised to NULL, then see the other answers, which basically say "if you want to init to pointer values, then init pointers, not ints".

Comments

1
#include <stdio.h>

typedef struct Segment {
    int microseconds;
} Segment;

typedef struct Pair {
    Segment mark;
    Segment space;
} Pair;

int main()
{
    Pair mark_and_space = { .mark.microseconds = (int) NULL, 
                            .space.microseconds =(int) NULL };
    return 0;
}

Ignoring all the other answers, if you still want to set it to NULL.

Comments

0

You're getting those errors, because NULL is a pointer. You cannot assign a pointer to a Segment. The way to fix this is to change the pair of Segments to Segment *s instead. But then you will be responsible for memory allocation.

Comments

0

Since Pair fields are not pointers NULL is not a good choice to initialize a structure. But if you want just to set fields to zero, you can try using more braces:

Pair mark_and_space = { .mark = {3}, .space = {0} };

This line my MinGW compiler complied without any warning. I set mark.milliseconds to 3 just for example how to set arbitrary initial value to internal milliseconds field.

Comments

0

You can't initialize a structure to null. You can initialize a structure pointer to null, though. Per the POSIX standard (emphasis mine):

NULL

Null pointer constant. [CX] ⌦The macro shall expand to an integer constant expression with the value 0 cast to type void *.

Note the type there, and why it's incompatible with your definition. You can either change the members to Segment * (and malloc your stack variables yourself), or you can statically initialize them:

Pair mark_and_space = { { }, { } };
/* v---------------------^    ^
  default to 0, but you can choose different value(s) */

2 Comments

Which standard are you reading? The C11 standard, ISO/IEC 9899:2011 §7.19 Common definitions <stddef.h> says: ¶3 The macros are: NULL which expands to an implementation-defined null pointer constant; …. That does not match what you quote. Neither C90 nor C99 says what you claim to quote; both said what C11 says. Your empty braces notation is also not valid in C — but is in C++. Are you quoting the C++ standard in a C question?
Oh, OK — you're quoting the POSIX standard, and missing out the markers which show that a rule is an extension over the C standard. I've add those markers, and stipulated that it is the POSIX standard that you're quoting and not the C standard, and suddenly your quotes are clean. The empty brace initializers are still not valid in standard C.
0
int main()
{
    Pair mark_and_space;
    mark_and_space.mark = NULL;
    mark_and_space.space = NULL;
    return 0;
}

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.