Re: NULL?? How come??
From: Bruno Desthuilliers (bdesth.nospam_at_removeme.free.fr)
Date: 11/03/03
- Next message: James Hu: "[OT] Contrapositive (was Re: [Xnews] Open Source Xnews?)"
- Previous message: Al Bowers: "Re: segmenatation fault while allocating memory"
- In reply to: mike79: "NULL?? How come??"
- Next in thread: Bruno Desthuilliers: "Re: NULL?? How come??"
- Reply: Bruno Desthuilliers: "Re: NULL?? How come??"
- Reply: Irrwahn Grausewitz: "Re: NULL?? How come??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: James Hu: "[OT] Contrapositive (was Re: [Xnews] Open Source Xnews?)"
- Previous message: Al Bowers: "Re: segmenatation fault while allocating memory"
- In reply to: mike79: "NULL?? How come??"
- Next in thread: Bruno Desthuilliers: "Re: NULL?? How come??"
- Reply: Bruno Desthuilliers: "Re: NULL?? How come??"
- Reply: Irrwahn Grausewitz: "Re: NULL?? How come??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|