指针数组
Example:(run environment:gcc-4.1.2 on linux 2.6.23.1)
pointerAndArray.c
#include <stdio.h>
#include <stdlib.h>
void pointerAndArray(void){
char *p[] = {"ab", "cde", "efgh"};
char q[][5] = {"ab", "cde", "efgh"};
char (*r)[5] = &q[1];
/*
char temp[5] = "abcde";
char (*r)[5] = &temp;
*/
/*char **t = {"ab", "cde", "efgh"};*/
printf("/n/n/nAbout the pointer, array, pointer array declaration/n");
printf("After the declaration and defination: char *p[] = {/"ab/", /"cde/", /"efgh/"}/n");
printf("&p = %#x/n", &p);
printf("p = %#x/n", p);
printf("/n");
printf("&p[0] = %#x/n", &p[0]);
printf("&p[1] = %#x/n", &p[1]);
printf("&p[2] = %#x/n", &p[2]);
printf("/n");
printf("p[0] = %#x/n", p[0]);
printf("p[1] = %#x/n", p[1]);
printf("p[2] = %#x/n", p[2]);
printf("/n");
printf("The string pointed to by char pointer p[0] is /"%s%s/n", p[0], "/"");
printf("The string pointed to by char pointer p[1] is /"%s%s/n", p[1], "/"");
printf("Ths string pointed to by char pointer p[2] is /"%s%s/n", p[2], "/"");
printf("/n");
printf("*p[0] = p[0][0] = %c/n", p[0][0]);
printf("*p[1] = p[1][0] = %c/n", *p[1]);
printf("*p[2] = p[2][0] = %c/n", *p[2]);
printf("/n");
printf("*++p[0] = %c/n", *++p[0]);
printf("*++p[1] = %c/n", *++p[1]);
printf("*++p[2] = %c/n", *++p[2]);
/*
After the declaration and defination char *p[] = {"ab", "cde", "efgh"},
Array name p is a constant value, can't be a left value, so ++p is illegal.
But p[0], p[1], [2] are variables of char pointer, so they can be left
value.
printf("++p = %#x/n", ++p); Error
*/
printf("++p[0] = %#x/n", ++p[0]);
printf("/n");
printf("After the declaration and defination char q[][5] = {/"ab/", /"cde/", /"efgh/"}/n");
printf("&q = %#x/n", &q);
printf("q = %#x/n", q);
printf("/n");
printf("&q[0] = %#x/n", &q[0]);
printf("&q[1] = %#x/n", &q[1]);
printf("&q[2] = %#x/n", &q[2]);
printf("/n");
printf("q[0] = %#x/n", q[0]);
printf("q[1] = %#x/n", q[1]);
printf("q[2] = %#x/n", q[2]);
printf("/n");
/*
After the declaration and defination q[][5] = {"ab", "cde", "efgh"},
q and q[0] are all constant value, so can't be left value.
printf("++q = %#x/n", ++q);
printf("++q[0] = %#x/n", ++q[0]);
*/
printf("/n");
printf("After the declaration and defination char (*r)[5] = &q[1]/n");
printf("&q[1] = %#x/n", &q[1]);
printf("q[1] = %#x/n", q[1]);
printf("The string in char array q[1] is : %s/n", q[1]);
printf("&r = %#x/n", &r);
printf("r = %#x/n", r);
printf("*r = %#x/n", *r);
printf("**r = %c , *(*r + 1) = %c , *(*r + 2) = %c/n", **r, *(*r + 1), *(*r + 2));
/*
After the declaration and defination char (*r)[5] = &q[1], r is a pointer variable pointed to an array with 5 char elements,
so ++r is legal, but ++*r is illegal.
printf("++*r = %#x/n", ++*r);
*/
printf("++r = %#x/n", ++r);
printf("/n");
}
void arrayArgsIsPointer(char p[]){
printf("About array actual parameter transmission/n");
printf("After the defination char arr[5] = /"abcde/", calling function (void arrayArgsIsPointer(char p[]))/n");
printf("by arrayArgsPointer(arr), the statement(printf(...sizeof(p)))/' results is /n%d(a pointer' size in 32-bits system)/n", sizeof(p));
printf("isn't 5(the arr' length)./n");
printf("This shows that when a function received a array actual parameter it will treat this actual parameter as a pointer./n");
printf("This situation will be true no matter the formal parameter is declarated as array(such as (char p[])) or pointer(such as char *p)./n");
}
pointer.h
#ifndef _pointer_H
#define _pointer_H
void pointerAndArray(void);
void arrayArgsIsPointer(char p[]);
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* _pointer_H */
main.c
#include "pointer.h"
#include <stdio.h>
#include <stdlib.h>
int main(void){
char arr[5] = "abcde";
arrayArgsIsPointer(arr);
pointerAndArray();
exit(0);
}
[---@---]$:gcc pointerAndArray.c main.c -o main.o
[---@---]$:./main.o
Result:
About array actual parameter transmission
After the defination char arr[5] = "abcde", calling function (void arrayArgsIsPointer(char p[]))
by arrayArgsPointer(arr), the statement(printf(...sizeof(p)))' results is
4(a pointer' size in 32-bits system)
isn't 5(the arr' length).
This shows that when a function received a array actual parameter it will treat this actual parameter as a pointer.
This situation will be true no matter the formal parameter is declarated as array(such as (char p[])) or pointer(such as char *p).
About the pointer, array, pointer array declaration
After the declaration and defination: char *p[] = {"ab", "cde", "efgh"}
&p = 0xbfb7f26c
p = 0xbfb7f26c
&p[0] = 0xbfb7f26c
&p[1] = 0xbfb7f270
&p[2] = 0xbfb7f274
p[0] = 0x8048a20
p[1] = 0x8048a23
p[2] = 0x8048a27
The string pointed to by char pointer p[0] is "ab"
The string pointed to by char pointer p[1] is "cde"
Ths string pointed to by char pointer p[2] is "efgh"
*p[0] = p[0][0] = a
*p[1] = p[1][0] = c
*p[2] = p[2][0] = e
*++p[0] = b
*++p[1] = d
*++p[2] = f
++p[0] = 0x8048a22
After the declaration and defination char q[][5] = {"ab", "cde", "efgh"}
&q = 0xbfb7f25d
q = 0xbfb7f25d
&q[0] = 0xbfb7f25d
&q[1] = 0xbfb7f262
&q[2] = 0xbfb7f267
q[0] = 0xbfb7f25d
q[1] = 0xbfb7f262
q[2] = 0xbfb7f267
After the declaration and defination char (*r)[5] = &q[1]
&q[1] = 0xbfb7f262
q[1] = 0xbfb7f262
The string in char array q[1] is : cde
&r = 0xbfb7f258
r = 0xbfb7f262
*r = 0xbfb7f262
**r = c , *(*r + 1) = d , *(*r + 2) = e
++r = 0xbfb7f267
#include <stdlib.h>
void pointerAndArray(void){
char *p[] = {"ab", "cde", "efgh"};
char q[][5] = {"ab", "cde", "efgh"};
char (*r)[5] = &q[1];
/*
char temp[5] = "abcde";
char (*r)[5] = &temp;
*/
/*char **t = {"ab", "cde", "efgh"};*/
printf("/n/n/nAbout the pointer, array, pointer array declaration/n");
printf("After the declaration and defination: char *p[] = {/"ab/", /"cde/", /"efgh/"}/n");
printf("&p = %#x/n", &p);
printf("p = %#x/n", p);
printf("/n");
printf("&p[0] = %#x/n", &p[0]);
printf("&p[1] = %#x/n", &p[1]);
printf("&p[2] = %#x/n", &p[2]);
printf("/n");
printf("p[0] = %#x/n", p[0]);
printf("p[1] = %#x/n", p[1]);
printf("p[2] = %#x/n", p[2]);
printf("/n");
printf("The string pointed to by char pointer p[0] is /"%s%s/n", p[0], "/"");
printf("The string pointed to by char pointer p[1] is /"%s%s/n", p[1], "/"");
printf("Ths string pointed to by char pointer p[2] is /"%s%s/n", p[2], "/"");
printf("/n");
printf("*p[0] = p[0][0] = %c/n", p[0][0]);
printf("*p[1] = p[1][0] = %c/n", *p[1]);
printf("*p[2] = p[2][0] = %c/n", *p[2]);
printf("/n");
printf("*++p[0] = %c/n", *++p[0]);
printf("*++p[1] = %c/n", *++p[1]);
printf("*++p[2] = %c/n", *++p[2]);
/*
After the declaration and defination char *p[] = {"ab", "cde", "efgh"},
Array name p is a constant value, can't be a left value, so ++p is illegal.
But p[0], p[1], [2] are variables of char pointer, so they can be left
value.
printf("++p = %#x/n", ++p); Error
*/
printf("++p[0] = %#x/n", ++p[0]);
printf("/n");
printf("After the declaration and defination char q[][5] = {/"ab/", /"cde/", /"efgh/"}/n");
printf("&q = %#x/n", &q);
printf("q = %#x/n", q);
printf("/n");
printf("&q[0] = %#x/n", &q[0]);
printf("&q[1] = %#x/n", &q[1]);
printf("&q[2] = %#x/n", &q[2]);
printf("/n");
printf("q[0] = %#x/n", q[0]);
printf("q[1] = %#x/n", q[1]);
printf("q[2] = %#x/n", q[2]);
printf("/n");
/*
After the declaration and defination q[][5] = {"ab", "cde", "efgh"},
q and q[0] are all constant value, so can't be left value.
printf("++q = %#x/n", ++q);
printf("++q[0] = %#x/n", ++q[0]);
*/
printf("/n");
printf("After the declaration and defination char (*r)[5] = &q[1]/n");
printf("&q[1] = %#x/n", &q[1]);
printf("q[1] = %#x/n", q[1]);
printf("The string in char array q[1] is : %s/n", q[1]);
printf("&r = %#x/n", &r);
printf("r = %#x/n", r);
printf("*r = %#x/n", *r);
printf("**r = %c , *(*r + 1) = %c , *(*r + 2) = %c/n", **r, *(*r + 1), *(*r + 2));
/*
After the declaration and defination char (*r)[5] = &q[1], r is a pointer variable pointed to an array with 5 char elements,
so ++r is legal, but ++*r is illegal.
printf("++*r = %#x/n", ++*r);
*/
printf("++r = %#x/n", ++r);
printf("/n");
}
void arrayArgsIsPointer(char p[]){
printf("About array actual parameter transmission/n");
printf("After the defination char arr[5] = /"abcde/", calling function (void arrayArgsIsPointer(char p[]))/n");
printf("by arrayArgsPointer(arr), the statement(printf(...sizeof(p)))/' results is /n%d(a pointer' size in 32-bits system)/n", sizeof(p));
printf("isn't 5(the arr' length)./n");
printf("This shows that when a function received a array actual parameter it will treat this actual parameter as a pointer./n");
printf("This situation will be true no matter the formal parameter is declarated as array(such as (char p[])) or pointer(such as char *p)./n");
}
pointer.h
#ifndef _pointer_H
#define _pointer_H
void pointerAndArray(void);
void arrayArgsIsPointer(char p[]);
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif /* _pointer_H */
main.c
#include "pointer.h"
#include <stdio.h>
#include <stdlib.h>
int main(void){
char arr[5] = "abcde";
arrayArgsIsPointer(arr);
pointerAndArray();
exit(0);
}
[---@---]$:gcc pointerAndArray.c main.c -o main.o
[---@---]$:./main.o
Result:
About array actual parameter transmission
After the defination char arr[5] = "abcde", calling function (void arrayArgsIsPointer(char p[]))
by arrayArgsPointer(arr), the statement(printf(...sizeof(p)))' results is
4(a pointer' size in 32-bits system)
isn't 5(the arr' length).
This shows that when a function received a array actual parameter it will treat this actual parameter as a pointer.
This situation will be true no matter the formal parameter is declarated as array(such as (char p[])) or pointer(such as char *p).
About the pointer, array, pointer array declaration
After the declaration and defination: char *p[] = {"ab", "cde", "efgh"}
&p = 0xbfb7f26c
p = 0xbfb7f26c
&p[0] = 0xbfb7f26c
&p[1] = 0xbfb7f270
&p[2] = 0xbfb7f274
p[0] = 0x8048a20
p[1] = 0x8048a23
p[2] = 0x8048a27
The string pointed to by char pointer p[0] is "ab"
The string pointed to by char pointer p[1] is "cde"
Ths string pointed to by char pointer p[2] is "efgh"
*p[0] = p[0][0] = a
*p[1] = p[1][0] = c
*p[2] = p[2][0] = e
*++p[0] = b
*++p[1] = d
*++p[2] = f
++p[0] = 0x8048a22
After the declaration and defination char q[][5] = {"ab", "cde", "efgh"}
&q = 0xbfb7f25d
q = 0xbfb7f25d
&q[0] = 0xbfb7f25d
&q[1] = 0xbfb7f262
&q[2] = 0xbfb7f267
q[0] = 0xbfb7f25d
q[1] = 0xbfb7f262
q[2] = 0xbfb7f267
After the declaration and defination char (*r)[5] = &q[1]
&q[1] = 0xbfb7f262
q[1] = 0xbfb7f262
The string in char array q[1] is : cde
&r = 0xbfb7f258
r = 0xbfb7f262
*r = 0xbfb7f262
**r = c , *(*r + 1) = d , *(*r + 2) = e
++r = 0xbfb7f267