Re: embedded questions!!!



On Fri, 13 Jan 2006 07:16:56 -0800 in comp.arch.embedded, "Richard
Henry" <rphenry@xxxxxxxx> wrote:

>
>"John B" <spamj_baraclough@xxxxxxxxxxxxxxxxxxx> wrote in message
>news:43c7b8d3$0$819$4c56ba96@xxxxxxxxxxxxxxxxxxxxxxxxx
>> On 12/01/2006 the venerable Chuck F. etched in runes:
[...]
>> The two declarations:
>>
>> char str1* = "JHONSON";
>>
>> and
>>
>> char str2[] = "JHONSON";
>>
>> both allocate space for a modifiable string. The difference being that the
>first one also creates a
>> pointer to that string.
>
>Isn't str2 also a pointer?

No.

Repeat after me: "Arrays are not pointers. Pointers are not arrays."
Despite what you may have been told.

A pointer is an object that references some other object.

An array is a group of contiguously allocated objects of the same
type.

(This is the important bit) The unadorned name of an array devolves
into a pointer to the first element of that array. Sort of like a
compile-time constant.

One point of confusion is the mechanism of array indexing. The syntax
"a[i]" has *exactly* the same meaning as "(*a + i)" whether a is a
pointer or array. So given the declarations above, str1[0] == 'J' ==
str2[0].

Another is the syntax for passing an array as a parameter to a
function. Thus "Fn(str1);" looks to be much the same as "Fn(str2);"
but the actual semantics are a bit different. Inside the function
itself, of course, the parameter is the same in either case.

>
>BTW, I will admit a recurring problem with understanding the nuances of C
>pointers.
>

Peter van der Linden's "Expert C Programming: Deep C Secrets" has one
or two excellent chapters on the differences between arrays and
pointers (as well as sections on understanding and contruction C
declarations, and other topics introductory books are to timid to
tackle). Somewhat dated (it was written about a dozen years ago), but
very readable and recommended. You don't read it because you're an
expert already, you read it to become expert...

Regards,
-=Dave

--
Change is inevitable, progress is not.
.



Relevant Pages

  • 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: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: how to return an array from function
    ... > I want to know a simple program of return array from function? ... ** Allocate space to hold _size_ ints, return pointer to ... void newa2 (size_t size, int **ap) ...
    (comp.lang.c)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)