Arrays & Pointers

A

arnuld

I am trying hard to understand the Arrays & Pointers relationship. Tell
me if I am wrong:


char a[] = "blah blah";
char *p = "blah blah";


----------------------
ARRAY | | | | | |
| | | | | |
----------------------

----------------------
p ---- ----- ----|-> | | | | |
| | | | | |
----------------------

this is the difference between an array and pointer to a string. Arrays &
Pointers are *not* interchangeable, they are *not* equal. When we pass an
array to a function, we are faking a pointer-phenomenon in reality because
arrays can not be passed to functions, when we pass an array as a
parameter to a function, it immediately decays into a pointer to its 1st
element and then we can access the subsequent elements using
pointer-arithmetic. That's all we got about Arrays and Pointers relation.
Except this they have nothing in common.


I am still not able to understand a program I wrote sometime ago, using th
example code I have shown above for a[] and *p. when I printed the array
using pointer-arithmetic, it printed all the elements but when I tried to
print the elements of that string using *p , it printed this:

^a^d^@
3 4


I did not understand it, it did not even print the one single element of
the string. I can't recall the exact code but it was a pointer to a string
and it printed exactly that output.




-- http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.
 
P

Philip Potter

arnuld said:
I am trying hard to understand the Arrays & Pointers relationship. Tell
me if I am wrong:


char a[] = "blah blah";
char *p = "blah blah";


----------------------
ARRAY | | | | | |
| | | | | |
----------------------

----------------------
p ---- ----- ----|-> | | | | |
| | | | | |
----------------------

this is the difference between an array and pointer to a string. Arrays &
Pointers are *not* interchangeable, they are *not* equal. When we pass an
array to a function, we are faking a pointer-phenomenon in reality because
arrays can not be passed to functions, when we pass an array as a
parameter to a function, it immediately decays into a pointer to its 1st
element and then we can access the subsequent elements using
pointer-arithmetic. That's all we got about Arrays and Pointers relation.
Except this they have nothing in common.

You seem to have the gist of it. Arrays and pointers are not the same
thing at all, but the existance of
pointers-to-the-first-element-of-arrays confuses matters, and people are
often sloppy and say that "ptr is a string" when they really mean "ptr
is a pointer to the first character of a string". Such sloppiness is not
always bad - but only when both parties know what the speaker /really/
means. In general, it's best to be specific.

Also, it's not just when you pass an array to a function that it decays
to a pointer to its first element. It's in almost every situation. For
example:

char a[] = "My natty array";
char *p;
p = a;
printf("%s\n",a);
printf("%s\n",p);
a[5] = 's';
p[5] = 's';
p = a + 3;

/* In every context above, a is treated as a pointer to a's first
element - in other words, a is treated as if it were &a[0]. */

printf("%zd\n",sizeof a); /* C99 only; use %d and cast to int for C89 */
printf("%zd\n",sizeof p);
char (*ptra)[15] = &a;
char **ptrptr = &p;

/* In the statements above, a does NOT decay to a pointer, hence the
differences. In general, sizeof a != sizeof p. &a is a pointer to an
array, &p is a pointer to a pointer. The following:
ptrptr = &a;
would NOT work because &a is not a char **, because a is not a char *. */

The situations in which an array does NOT decay are: as an argument to
the sizeof or unary & operator, or as a string literal initializer to a
char array.
I am still not able to understand a program I wrote sometime ago, using th
example code I have shown above for a[] and *p. when I printed the array
using pointer-arithmetic, it printed all the elements but when I tried to
print the elements of that string using *p , it printed this:

^a^d^@
3 4


I did not understand it, it did not even print the one single element of
the string. I can't recall the exact code but it was a pointer to a string
and it printed exactly that output.

I can't say for sure without seeing the code. Did you pass p (not *p) to
printf?
char *p = something;
printf("%s\n",*p); /* WRONG */
printf("%s\n",p); /*correct*/

%s expects a char *, not a char. p is a char *, *p is what p points to,
and hence a char. If you pass a char where %s expects a char *, it gets
confused.

If this *is* your problem, then turn up the warning level on your
compiler. Many compilers automatically check printf() argument lists to
ensure the types of arguments match the format string specifiers. For
example:

pgp@bullfinch:~/tmp$ cat foo.c
#include <stdio.h>
int main(void) {
printf("%s\n",3);
return 0;
}
pgp@bullfinch:~/tmp$ gcc -ansi -pedantic -W -Wall -ofoo foo.c
foo.c: In function 'main':
foo.c:3: warning: format '%s' expects type 'char *', but argument 2 has
type 'int'
pgp@bullfinch:~/tmp$
 
D

David Thompson

I am trying hard to understand the Arrays & Pointers relationship. Tell
me if I am wrong:


char a[] = "blah blah";
char *p = "blah blah";


----------------------
ARRAY | | | | | |
| | | | | |
----------------------

----------------------
p ---- ----- ----|-> | | | | |
| | | | | |
----------------------

this is the difference between an array and pointer to a string. Arrays &
Pointers are *not* interchangeable, they are *not* equal. When we pass an
array to a function, we are faking a pointer-phenomenon in reality because
arrays can not be passed to functions, when we pass an array as a
parameter to a function, it immediately decays into a pointer to its 1st
element and then we can access the subsequent elements using
pointer-arithmetic. That's all we got about Arrays and Pointers relation.
Except this they have nothing in common.
I think you've got the idea, but:

- I would be careful about using 'equal'. Pointers and arrays are
different things, but they only way in C to determine equality of two
things is an expression using the == operator, and _in an expression_
an array turns into a pointer which when compared to another pointer
can indeed be equal. Since your sig refers to lispmachine, I'll point
out that LISP makes a similar distinction between EQ and EQUAL.

They actually are interchangeable _sometimes_, but not all. I like to
say 'not identical', or just 'not the same'.

- also, I wouldn't say that passing an array to a function, or in most
other uses, is 'faking' a pointer. The array (lvalue) is _converted_
to a pointer (rvalue), which is then just as good a pointer value as
any other. Maybe 'creating' or 'producing'. Or the general term for
the result of any expression/operation, 'yielding'.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top