ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
SOLVED: c gurus: whats the diff? "var[]" and "*var"?
I have used "char *var" and "char var[]" more or less interchangeably until it stopped working recently. I have seen the following problem on two occasions. The first time I thought it was a buggy compiler, now I think I might be missing a crucial piece of knowledge.
I have a function which refers to an external array of chars, very plan and boring. The following works:
I have used "char *var" and "char var[]" more or less interchangeably until it stopped working recently. I have seen the following problem on two occasions. The first time I thought it was a buggy compiler, now I think I might be missing a crucial piece of knowledge.
I have a function which refers to an external array of chars, very plan and boring. The following works:
Code bugs are nearly always down to the programmer.
You should always assume that the fault is with your code until you have exhausted every possibility.
char buffer[]="hello mum" allocates enough memory to store the string plus its null terminator.
thus you can reference buffer[0] or buffer[1] or buffer[4]
up until buffer[strlen(buffer)] where buffer[(strlen(buffer)]
will = '\0'
however...
Code:
extern volatile char *myvar;
you havn't inititialised *myvar to point to any memory within the memory space of your program.
Unless you have done so somewhere else in your code???
Thus it is a wild pointer pointing to some random location in memory,most probably a locaton in memory that your pogram dosn't have access to.
If you declare a pointer to char you have to make sure that it then points to something meaningful,i.e legal memory.
execution log: $ a
a says:
&myarray0=0x0804974c
&myarray1=0x08049750
aa says:
&arg0=0x0804974c
&arg1=0x08049750
b says:
&myarray0=0x006f6f66
&myarray1=0x08049750
I've written functions like aa many times, but somehow, in 20+ years of C programming, I've never had occasion to use the "extern char *" line before. Actually I know why I've never used it before, I purposely avoided allowing one module to meddle with another's variables, but in the situation I'm in now, I can't avoid it.
So, the question is, why is there a semantic difference between the "char *" reference in the aa function's arglist and the "char *" reference in b's extern var list?
Last edited by kourama; 03-25-2007 at 04:59 PM.
Reason: formatted code
I guess the way you were doing it the compiler was interpreting it as your declaring another external variable instead of referencing it against the previously declared one?!!
I guess the way you were doing it the compiler was interpreting it as your declaring another external variable instead of referencing it against the previously declared one?!!
If you look at the value that b gives for the address of myarray0, it is in fact the contents of the array (0x006f6f66 is "foo\0" in little-endian format). For some reason it dereferences the variable and returns value instead of address. If I refer to "&myarray0" instead of just "myarray0" I'll get the correct address of myarray0; this seems wrong to me.
Quote:
Originally Posted by slzckboy
p.s
pls try and use format tags wen posting your code.Thnks
But this is not allowed, as you have not assigned memory to the pointer:
Code:
char *string3;
*string3 = "Hello World";
--Ian
actually, what you're doing is valid, you just have a syntax error, what you want is:
char *string3;
string3="Hello World";
when you link, the "Hello World" string is stored in the .rodata section, and the variable string3 points to it. no problemo. Remeber that "Hello World" returns a pointer to that string, which is why your example above with string1 and string2 work. When you are declaring the variable, the '*' indicates that you mean a pointer. In the body of the code '*' means that your are derefrencing that pointer.
Quote:
Originally Posted by bigearsbilly
and you don't know the difference?
are you sure?
it's quite simple,
char buffer[100] is a buffer of memory that can store 100 bytes.
char *p is a pointer that typically points at a char buffer or array.
you must make sure that what *p points at is a valid buffer.
Yes, there's a conceptual difference in your example, but in the compiled code both are simply pointers to locations in memory. The "[100]" tells the linker to allocated enough space in the .bss or .data sections of your program (depending on whether or not it's initialized) and tells the compiler how many elements exist so that it can check to see if you do something bad like make a reference to buffer[125].
I appreciate the responses guys, but please pay attention to the example. There is no issue with initialization, the issue is that "char *" means something different when used to declare a reference to an external variable in b.c and when used to declare a reference to an argument in the aa function in a.c. Why is there a different meaning assigned when the context strongly suggests (IMHO) that they should be identical?
Maybe I should contact the GCC developers group on this one.
This is not good. Nobody mentioned the C99 standard and what it says:
1. char var[]="abc";
This creates an array of char which is modifable
2. char *var="abc";
This creates an array of char, the results of modifying it are unspecified.
FYI "unspecified" is a code word for "It may or may not work. But don't expect to be able to port the code just because some compiler writer was nice enough to make it work for your platform."
In short "don't modify it"
3. char *var=NULL; [ or char *var; ] is a pointer to char and is modifable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.