Re: NULL?? How come??

From: Bruno Desthuilliers (bdesth.nospam_at_removeme.free.fr)
Date: 11/03/03


Date: Mon, 03 Nov 2003 13:43:11 +0100

mike79 wrote:
> Hi all,
>
> I have a the following simple piece of code which has taken me hours
> to try and sort out the problem, but still unable to find what is
> wrong.
>
> void main( void )

it's int main(void) or int main(int argc, char **argv).

> {
> char (*string)[20]; .......................(1)

Not an expert, but I'm not sure the above is correct (any guru out there
?).

But well, let's pretend it is and it really means what you want...
          
> string = malloc(10 * sizeof *string); .....(2)
> string[0] = NULL; .........................(3)
> }

> Basically, I am allocating memory to an array of strings of 20 chars
> max, and setting element 0 of the array to be a NULL value.

> Lines (1) to (2) work OK, but line (3) does not work. I wish to set
> the string of element 0 to be a NULL value, but keeps getting an error
> saying:

> "left operand must be l-value"
> Please help me, I dont understand what's wrong.

<gurus, please correct me if I'm wrong>

This last line is basically the same as writing :
int main(void)
{
     char str20[20];
     str20 = NULL;
     return 0;
}

This gives me (gcc 3.x, with -Wall -ansi -pedantic switches) :
'incompatible types in assignment'

This does not surprise me, since, while sharing some mechanisms, an
automatic array and a pointer to a dynamically allocated memory block
are two different things.

You can consider str20 as a pointer to the first element of a 20 chars
array (and yes it is, technically), but it's a 'read-only' pointer, in
the meaning that you *can not* modify the *address* it points to.

If what you want is to init each element in the 'string' array to the
empty string, you may want to have a look at calloc() or memset() :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
     char (*strings)[20];
     int i;
     int j;
     size_t size = 10 * sizeof *strings;

     strings = malloc(size);
     memset(strings, 0, size);

     for (i = 0; i < 10; i++) {
         printf("strings[%d] : \n", i);
         for (j = 0; j < 20; j++) {
             printf(" '%c' ", strings[i][j] + '0');
         }
         printf("\n");
     }

     free(strings);
     return 0;
}

Note that it *may* not be necessary (at least it *seems* not to be on
Linux with gcc 3.2.2 : commenting out the 'memset()' call doesn't seems
to change the output), but as this could be an UB, I would not rely on
this without the enlightening advices of some guru here (any guru around ?-)

HTH,
Bruno



Relevant Pages

  • Re: Warning on assigning a function-returning-a-pointer-to-arrays
    ... This declares pfunc as a function taking no arguments and returning ... int x, y; ... Presumably pfuncwill return a pointer to a single int, ... or the first of a sequence of "array 5 of int"s. ...
    (comp.lang.c)
  • Re: Newbie
    ... to talk about the int value 3 and the int value 4, ... It also lets you talk about pointer ... C has a special rule for array objects. ... to printf() is: ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: union {unsigned char u[10]; ...}
    ... But character type is not a union. ... u.a is of type int. ... has to do so to make pointer equality work consistently). ... were a single-element array. ...
    (comp.lang.c)
  • Re: How to pass a pointer to an unknown-size array?
    ... > I can pass a "pointer to a double" to a function that accepts ... > int func{ ... > Now I want to pass a pointer to an array of doubles, ...
    (comp.lang.c)