Re: How does this c - code work

From: Dave Neary (real.addressinthesig_at_thisoneis.invalid)
Date: 09/13/04


Date: 13 Sep 2004 21:02:41 GMT

Hi,

On 13 Sep 2004 02:52:00 -0700, David said:
> #include <stdio.h>
>
> int main()
> {
> char str[] = "Hello";

str is an array of characters (this is not the same as a char pointer,
as we will see).

> printf("%s\n", str);

When an array is used in an expression, it evaluates to a pointer to its
first element. In this case, you are passing &str[0] to printf (which is
fine, str[0] is a char *, which goes fine with the %s).
 
> printf("%s\n", &str);

Here, you are passing a pointer to the array (note the difference).
For example,
printf ("%s\n", &str + 1);
will pront garbage, because incrementing a pointer points to the next
object after the current one. Since the pointer is a pointer to an array
of size 6, you are actually going forward 6 bytes.

printf ("%s\n", str + 1);
would print "ello", though, since incrementing a char * goes on to the
next char.

However, since the address of the array is the same as the address of
its first member, the two evaluate to the same pointer value. When we
pass &str to printf, the %s format specifier means that printf
"pretends" that &str is a char *. So in printf, we end up with &str
being (char *)&str, which is more or less the same thing as str (or
&str[0], except that it's kind of undefined behaviour what happens when
you dereference an object not of type char* as a char*. However, doing
so as unsigned char * is fine. That said, you're usually in safe waters
with character types.

Cheers,
Dave.

-- 
             David Neary,
     E-Mail: bolsh at gimp dot org
Work e-mail: d dot neary at phenix dot fr
     CV: http://dneary.free.fr/CV/


Relevant Pages

  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: pointer and array
    ... >The two yield incompatible pointer types. ... char", pointing at the first char in the array. ... &str has type "pointer to array of char", ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)