-2

Yes I did read these two posts.

C compile error: "Variable-sized object may not be initialized" C error "variable-sized object may not be initialized"

My case is a bit different because I'm using char * ptr[buflen]. This is what I have tried:

char  *ptr[buflen] = {0};  //This gave me the variable sized object error. 

char  *ptr[buflen];
memset( ptr, 0, buflen*buflen*sizeof(char)); //I figured this would work with looking at the previous examples. 

//This seemed to work but I am curious if I need to use free or malloc
after looking at the previous examples. I don't want this to seg fault later
in the program and have no clue what is causing it. 
char  *ptr[buflen]; 
memset(ptr, 0, sizeof ptr);

char *strings_line_tokens[503] = {0}; //Why does this work but the above won't work? 
Community
  • 1
  • 1
rocker
  • 1
  • 3
  • 2
    `memset(ptr, 0, buflen*sizeof(char *))`. Though *technically* a NULL-ptr doesn't have to be zero, so you should *probably* just use a loop to initialize all pointers. – EOF Oct 08 '15 at 19:07
  • 1
    `char *ptr[buflen] = {0}` should be `char *ptr[buflen] = {NULL}` – David C. Rankin Oct 08 '15 at 19:09
  • What are you going to store in `ptr` I think you are confused about that. – Iharob Al Asimi Oct 08 '15 at 19:09
  • 1
    @DavidC.Rankin: Why the bikeshedding? It's the same thing in this context. – EOF Oct 08 '15 at 19:09
  • Yes, I know that, but I"ve seen compilers complain if you try an initialize an array of pointers to `0` instead of `NULL`. Which compiler would help. – David C. Rankin Oct 08 '15 at 19:10
  • 1
    @DavidC.Rankin: Sounds like you've used a broken compiler. – EOF Oct 08 '15 at 19:11
  • 3
    Yes but you can't initialize a variable length array anyway! – Iharob Al Asimi Oct 08 '15 at 19:11
  • @DavidC.Rankin, a real C compiler should never complain about that. A C++ would probably, but C is different. – Jens Gustedt Oct 08 '15 at 19:11
  • @iharob has the primary concern, I think that is probably the main issue. – David C. Rankin Oct 08 '15 at 19:12
  • @rocker, why is your case different from the one that you have in the link? How does the answer that is given there not give you enough information to figure things out? – Jens Gustedt Oct 08 '15 at 19:14
  • @JensGustedt I don't know if I need to use free or malloc. They both could be important with what I'm doing. I missed the `*` at the end of `memset(ptr, 0, buflen*sizeof(char *))`. – rocker Oct 08 '15 at 19:21
  • For the arrays themselves as you define them you wouldn't need dynamic allocation through `malloc`, this is the whole idea of variable length arrays. But then you have arrays of pointers, but didn't tell us what you want to do with them, so we can't know. – Jens Gustedt Oct 08 '15 at 19:26
  • @iharob `ptr` will contain an array of strings. I needed to check if it contained a new line character or null character. When I tried do that it segfaulted because it hadn't been initialized. So I tried to initialize this. And that is why I would like to know if I need malloc or free. – rocker Oct 08 '15 at 19:28
  • No it will contain an array of pointers, if the pointers point to strings that's another thing. – Iharob Al Asimi Oct 08 '15 at 19:28
  • @iharob I fill it with strings. – rocker Oct 08 '15 at 19:32

1 Answers1

0

My case is a bit different because I'm using char * ptr[buflen].

Not really. You're declaring ptr as a buflen-sized array of char *. If buflen is not a constant expression, then ptr is a variable-length array, and you may not use an initializer for a variable-length array:

6.7.9 Initialization
...
3 The type of the entity to be initialized shall be an array of unknown size or a complete object type that is not a variable length array type.

Emphasis added.

So, quite simply,

char *ptr[buflen] = {0};

is not a legal initialization.

If you want to initialize all the elements of ptr to 0 or NULL, you would use memset:

memset( ptr, 0, sizeof *ptr * buflen ); // note memset writes 0 to all *bytes* in 
                                        // in the array 

or you would assign each element in a loop:

for ( size_t i; i < buflen; i++ )
  ptr[i] = NULL; // or 0, or some other pointer value
John Bode
  • 119,563
  • 19
  • 122
  • 198
  • So this didn't do what I expected `memset(ptr, 0, sizeof ptr);`? It didn't give me any compiler warning and didn't segfault. – rocker Oct 08 '15 at 19:39
  • @rocker: bugger. Let me fix that. – John Bode Oct 08 '15 at 19:59
  • @rocker: my fault - that line should be `memset( ptr, 0, sizeof *ptr * buflen);`. I've been working with pointers to arrays vs. arrays of pointers lately and got my wires crossed. – John Bode Oct 08 '15 at 20:00