C Structure using Pointer as Array

Programming, for all ages and all languages.
Post Reply
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

C Structure using Pointer as Array

Post 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?
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: C Structure using Pointer as Array

Post by CodeCat »

Not sure why it happens, but a solution could be to simply put an & before the string literal.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C Structure using Pointer as Array

Post 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.
Every good solution is obvious once you've found it.
CodeCat
Member
Member
Posts: 158
Joined: Tue Sep 23, 2008 1:45 pm
Location: Eindhoven, Netherlands

Re: C Structure using Pointer as Array

Post 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.
User avatar
thepowersgang
Member
Member
Posts: 734
Joined: Tue Dec 25, 2007 6:03 am
Libera.chat IRC: thePowersGang
Location: Perth, Western Australia
Contact:

Re: C Structure using Pointer as Array

Post by thepowersgang »

#-o #-o

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.
Kernel Development, It's the brain surgery of programming.
Acess2 OS (c) | Tifflin OS (rust) | mrustc - Rust compiler
Currently Working on: mrustc
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: C Structure using Pointer as Array

Post 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. ;)
Every good solution is obvious once you've found it.
iammisc
Member
Member
Posts: 269
Joined: Thu Nov 09, 2006 6:23 pm

Re: C Structure using Pointer as Array

Post by iammisc »

instead of using %x, why not %p?
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: C Structure using Pointer as Array

Post 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
Post Reply