char *str vs. char str[] 
Author Message
 char *str vs. char str[]

Hi,

I wrote a program consisting of several C source files. In one of
the files I defined a global variable that I wanted to access to
in one of the other files as well:

file 1:                         file 2:
-------                         -------

[...]                           [...]
char str[100];                  extern char *str;
[...]                           [...]
                                puts(str);
                                [...]

But printing the string in file2 resulted in some garbage on the
screen. Searching for the error I found out that after changing
"extern char *str" into "extern char str[]" the string was printed
properly.

Who knows why? So far I thought that "extern char *str" and
"extern char str[]" would be equivalent expressions.
Obviously they aren't.

Any help is welcome!

Mathias

----------

Mathias Krallmann, Kossmannstr. 1, D-6600 Saarbruecken, Germany



Sat, 23 Sep 1995 17:56:51 GMT  
 char *str vs. char str[]

: file 1:                               file 2:
: -------                               -------

: [...]                         [...]
: char str[100];                        extern char *str;
: [...]                         [...]
:                               puts(str);
:                               [...]

This is very wrong.

char str[100] defines an area of 100 bytes in memory that will
contain a series of characters.
char *str however will define an area of 4 bytes which would
hopefully contain the *address* of a series of characters.

When you do what you just did, you are telling file2 to interpret
the first 4 characters of str[] as the address of a string.
For example, if str[]="AAAA", then when you try to printf *str,
it'll print the contents of memory position #41414141, which most
likely will contain garbage.

: Who knows why? So far I thought that "extern char *str" and
: "extern char str[]" would be equivalent expressions.
: Obviously they aren't.

You're right.

----------------------------------------------------------------------------
                      |       Alex Ramos       | Fala-se Portugues, tche!

                      |      LA Tech Univ.     | English spoken here, barely
----------------------------------------------------------------------------



Sun, 24 Sep 1995 03:18:46 GMT  
 char *str vs. char str[]

: I wrote a program consisting of several C source files. In one of
: the files I defined a global variable that I wanted to access to
: in one of the other files as well:
:
: file 1:                               file 2:
: -------                               -------
:
: [...]                         [...]
: char str[100];                        extern char *str;
: [...]                         [...]
:                                 puts(str);
:                                 [...]
:
: But printing the string in file2 resulted in some garbage on the
: screen. Searching for the error I found out that after changing
: "extern char *str" into "extern char str[]" the string was printed
: properly.
:
: Who knows why? So far I thought that "extern char *str" and
: "extern char str[]" would be equivalent expressions.
: Obviously they aren't.

They aren't. :-)  "char *str" and "char str[]" are equivalent *only*
when they appear as formal parameters in a function definition.

Do not confuse it with a variable declaration.

"str" of "char *str" is a lvalue: can be assigned to, whereas "str"
of "char str[]" is not.  They are compiled into different codes.

In file 1, do you say "char *str" instead of "char str[100]" ?
I suppose you know the difference.  It is the same thing even if files
are separated.  Once you say "char str[..]" in a file, you must
declare "extern char str[]" in others.

This kind of bug is hard to find because neither compiler nor linker
complains about the type difference (even if you say "extern double
*str" in file 2).  Lint will tell you about that, viz.

        value type declared inconsistently:

or something like that.

Hope this helps.
--
Toru "devil-may-care" Shiono          Sony Corporation, JAPAN



Sun, 24 Sep 1995 08:55:36 GMT  
 char *str vs. char str[]

Quote:

}Who knows why? So far I thought that "extern char *str" and
}"extern char str[]" would be equivalent expressions.
}Obviously they aren't.

Correct, they aren't. 'char str[]' is an array of char, and allocates space.  
'char *str' is a pointer to a char data type, and does not.
--
Matthew Lee Stier                          |
Sun Microsystems ---  RTP, NC  27709-3447  |     "Wisconsin   Escapee"

Phone: (919) 469-8300 fax: (919) 460-8355  |


Sat, 23 Sep 1995 18:42:20 GMT  
 char *str vs. char str[]

Quote:
>Correct, they aren't. 'char str[]' is an array of char, and allocates space.  
>'char *str' is a pointer to a char data type, and does not.

You say that 'char str[]' allocates space.  'char str[80]' allocates space,
but 'char str[]'?  How does the compiler know how much to allocate?

Even in initializing a string constant (the way I understand it)
'char str[] = "hello";', str is just a pointer to the space somewhere
else containing the string.

Am I looking at this wrong?
--
Steve Bement



Mon, 25 Sep 1995 22:24:48 GMT  
 char *str vs. char str[]

Quote:
>Even in initializing a string constant (the way I understand it)
>'char str[] = "hello";', str is just a pointer to the space somewhere
>else containing the string.

No. str is an array. The declaration you gave is completely
synonymous with

char str[6] = "hello";

or

char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};

That is, it declares str to be a character array, not a pointer.

If you want str to be a pointer to the beginning of an anonymous
array of characters which spell out "hello", then declare str
to be a pointer. This is done as follows:

char *str = "hello";

A pointer is not an array and an array is not a pointer.

--
Dave Eisen                               "To succeed in the world, it is not

Sequoia Peripherals: (415) 967-5644       be well-mannered." --- Voltaire
Home:                (415) 321-5154  



Tue, 26 Sep 1995 14:24:36 GMT  
 char *str vs. char str[]
*> Hi,
*>
*> I wrote a program consisting of several C source files. In one of
*> the files I defined a global variable that I wanted to access to
*> in one of the other files as well:
*>
*> file 1:                           file 2:
*> -------                           -------
*>
*> [...]                             [...]
*> char str[100];                    extern char *str;
*> [...]                             [...]
*>                           puts(str);
*>                           [...]
*>
*> But printing the string in file2 resulted in some garbage on the
*> screen. Searching for the error I found out that after changing
*> "extern char *str" into "extern char str[]" the string was printed
*> properly.
*>
*> Who knows why? So far I thought that "extern char *str" and
*> "extern char str[]" would be equivalent expressions.
*> Obviously they aren't.
*>
*> Any help is welcome!

From file1, str is set to be an array of 100 char elements.  The
address is set up, and str is that address.  In file2, str is a pointer
to a char element.  Note that it is a variable which needs to have an
address value assigned to it.  Since it does not have an associated
assignment of value, it is natural that what you access will be garbage.
The extern str[] says that str is an array of unknown quantity of chars,
where str is already set up with an address, since it is declared to be
an array.  Notice the difference in saying something is a pointer to
an element, and something is an array of elements.  The array gets
"bound" to a specific address (much like when declaring any other data
type, eg int).  Thus, when you access str[] the compiler already knows
where the array lives by using str.  But when char *p is declared, p is
dereferenced to get access to the char pointed to.

OK??

*>
*> Mathias
*>
*> ----------
*>
*> Mathias Krallmann, Kossmannstr. 1, D-6600 Saarbruecken, Germany

--

Data General Corporation, Linthicum, MD



Tue, 26 Sep 1995 19:22:22 GMT  
 char *str vs. char str[]

Quote:

>char *str = "hello";
>A pointer is not an array and an array is not a pointer.

Yes, but do this only if you like wasting space and time.
--
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Cognisys Software Systems Development, San Jose, CA 95129
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


Fri, 29 Sep 1995 14:51:29 GMT  
 char *str vs. char str[]

Quote:


>>char *str = "hello";
>Yes, but do this only if you like wasting space and time.

Huh?  Why is it a waste of time to do something like:

char *str = "yes";
...
/* complicated series of tests that may change str to point to "no" */
printf("The answer is %s\n", str);

Yes, a complicated series of '?:' operators may do the same thing without
requiring the pointer but who's wasting time then?

--

D'Arcy Cain Consulting              |   There's no government
Toronto, Ontario, Canada            |   like no government!
+1 416 424 2871          DoD#0082   |



Sat, 30 Sep 1995 19:26:58 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char s

3. char* and char str[2]

4. _T(str) vs (CString)str?

5. c standard regarding char str[ ], str[50] & *str

6. Basic Question - char *str V/S int *

7. str != (char *) NULL

8. register char str[LEN]

9. how to address char * str[] ?

10. char *str - Pointer usage

11. q about array of char str

12. Dialogs: m_myCtrl.GetWindowText(str) or GetDlgItemText(IDC_MY_CTRL, str)?

 

 
Powered by phpBB® Forum Software