Page 1 of 1
C Structure using Pointer as Array
Posted: Sat Jan 17, 2009 9:55 am
by thepowersgang
I'm trying to fill a structure using the {} method but the string contained in the first field does not seem to store as a pointer to the string, but actually stores the string's data IN the pointer.
Code: Select all
...
typedef struct {
char *Name;
tClass *Classes;
int ClassCount;
} tNamespace;
...
tNamespace gaCommonNamespaces[] = {
{"Stdlib", gaCommonClasses_Stdlib, sizeof(gaCommonClasses_Stdlib)/sizeof(tClass)},
{"System", gaCommonClasses_System, sizeof(gaCommonClasses_System)/sizeof(tClass)}
};
..
When I do
Code: Select all
printf("String Pointer address = 0x%x\n", gaCommonNamespaces[0].Name);
I get the value 0x6C647453, which seems to be the ASCII representation of the letters 'Stdl' instead of a pointer.
Does anyone know why this happens?
Re: C Structure using Pointer as Array
Posted: Sat Jan 17, 2009 10:20 am
by CodeCat
Not sure why it happens, but a solution could be to simply put an & before the string literal.
Re: C Structure using Pointer as Array
Posted: Sat Jan 17, 2009 12:29 pm
by Solar
Guessing, and trial & error, are very poor guides in the realm of programming.
Without seeing the rest of your code, or knowing anything about your environment, it could be anything from a syntax error somewhere to a borked printf() implementation...
Can you give a minimal example with which the problem can be reproduced? (Usually the first step towards finding out what's going on.) With that I mean a minimal int main() that compiles on your machine, exhibits the problem, and has been copied into your webbrowser verbatim.
Re: C Structure using Pointer as Array
Posted: Sat Jan 17, 2009 5:05 pm
by CodeCat
It doesn't have anything to do with his environment really. This is presumably behaviour that is standard to the C language itself. When you initialise a struct, you provide values for each of its members. Structs can also contain arrays, which can be initialised in the same way:
Code: Select all
struct A
{
char stuff[10];
};
struct A foo = {"test!"};
Afterwards, foo.stuff will be an array containing the string "test!".
However, when you provide an array to initialise a member with, the compiler has no idea whether you intended to initialise that member with the array, or with a pointer to that array. It therefore copies the array itself into the struct, rather than a pointer to it, even if the types don't match. I've no idea why the C standard allows this, but apparently it happens.
Re: C Structure using Pointer as Array
Posted: Sat Jan 17, 2009 7:48 pm
by thepowersgang
I just found the bug, I was defining gaCommonNamespaces as a pointer instead of an array in the file that did the printf. My excuse is that I was tired when I wrote it.
Re: C Structure using Pointer as Array
Posted: Sun Jan 18, 2009 5:22 am
by Solar
CodeCat wrote:It doesn't have anything to do with his environment really. This is presumably behaviour that is standard to the C language itself.
No.
However, when you provide an array to initialise a member with, the compiler has no idea whether you intended to initialise that member with the array, or with a pointer to that array.
In his example the member was a char *, not a char [], so initializing that member with a string literal is a well-defined operation for the compiler: Have the member pointer point to somewhere in the .rodata (or .data) section where the string literal resides.
I've no idea why the C standard allows this, but apparently it happens.
It doesn't, and it doesn't.
Re: C Structure using Pointer as Array
Posted: Fri Jan 23, 2009 12:28 am
by iammisc
instead of using %x, why not %p?
Re: C Structure using Pointer as Array
Posted: Fri Jan 23, 2009 11:01 am
by DeletedAccount
%x -> for hex values
%p -> for pointers
hint : Dependending upon scanf/printf for type conversion is a big error , and may print out garbage value at times .
Regards
Shrek