Visit Our UNIX and Linux User Community

Pointers and array

Thread Tools Search this Thread
Top Forums Programming Pointers and array
# 15  
Old 09-25-2013
Thanks Shamrock!
Good hints are
&a[1][0] is a pointer to array of 2 chars..
As a[1] is an array of 3 arrays each made up of an array of 2 chars
So, a[1] is:{{'g', 'h'}, {'i', 'j'}, {'k', 'l'}}
Read more last night, and finally I got the answers, just for somebody who hits the same wall as I did.

as (*pa)[2] = &a[1][0], which means *pa is a "2-col" pointer start from {'g', 'h'} in the array a; so that char 'r' is in {'q', 'r'}, the 6th member of pa ---> pa[5], which in turn is a "2-char" array, and with the second index = 1. So the answer is pa[5][1];
as (*ppa)[3][2] = &a[1], which defines *ppa is a "3(row) x 2(col) array " pointer start from {{'g', 'h'}, {'i', 'j'}, {'k', 'l'}} in the array a. For char 'r' is on the 2nd row, 3rd col, within the 3rd col it is in the 2nd col. So the answer is ppa[1][2][1].

The reason why there is multiple answer to get the 'r' is because C actually only has "1-dimension" or linear arrangement of the array members in memory as Corona688 pointed out. When you split the linear array in different combinations, counted by 1, by 2, or by 3, 4, 5.... Actually pa[11] prints 'r', pa[3][2], pa[4][3] pa[5][1]...all point to 'r', but only pa[5][1] is appropriate to the original question. Similar for ppa, but only pa[1][2][1] is the correct answer as it was defined as a 3x2 array.

Shamrock and Corona688, please correct me if I interpreted anything wrong.
Thanks a lot again!

---------- Post updated 09-25-13 at 12:50 PM ---------- Previous update was 09-24-13 at 05:50 PM ----------

To confirm my digestion I tried another two situations by change the pa and ppa definitions:
char (*pa)[2] = &a[0][1];
char (*ppa)[3][2] = &a[3];
the answers to get char 'r' are

Here it is very interesting the index can be negative. Amazing C!

Last edited by yifangt; 09-25-2013 at 01:32 PM.. Reason: typos
# 16  
Old 09-25-2013
Originally Posted by yifangt
Here it is very interesting the index can be negative. Amazing C!
That's just how the processor, and two's-complement math works, not a special property of C per-se. It lets you do so, when most languages would protect you from it, but C, which hardcodes so much into assembly language at compile-time (same reason sizeof() can't be used dynamically) is very limited in what it can warn you about.

In most situations, a negative array index would edge into incorrect or even invalid memory, causing strange behavior or crashes. It's only okay here since you know you're not at the beginning of the data of interest.

So, use with care.
# 17  
Old 09-25-2013
Sure! It was only to confirm my understanding on the pointer to multi-dimension array. Thanks a lot!
# 18  
Old 09-25-2013
Originally Posted by yifangt
Thanks Corona688!
I fully understand the full array is actually a big 1-dimension array from your previous reply.
I tried:
printf("The value in a[1] is: %p\n", a[1]);
    printf("The value in a[0] is: %p\n", a[0]);
    printf("The value in a[1][0] is: %p\n", a[1][0]);
    printf("The value in a[1][1] is: %p\n", a[1][1]);

The value in a[1] is: 0x7fff80c346a6
The value in a[0] is: 0x7fff80c346a0
The value in a[1][0] is: 0x7fff80c346a6
The value in a[1][1] is: 0x7fff80c346a8

The value in a[1][0][0] is: g
The value in a[1][0][1] is: h

first 4 printf() print addresses in fact, and I seem understand that:
Because a[1] is the first address of a two-way (3x2) array, so a[1] is the same as a[1][0], which in turn is the first address of two-elements array, so that (now value, not address) a[1][0][0] = g; a[1][0][1] = h.
a[1] is not the first address of the 2-d is the name of the 2-d array of rank 3x2...and since the name of an array is a synonym for the location of its initial element hence it contains the address of the second 2-d array of rank 3x2.

a[0] is this array -> {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}
a[1] is this array -> {{'g', 'h'}, {'i', 'j'}, {'k', 'l'}}
Originally Posted by yifangt
Thanks Corona688! My confusion is: How is the connection with the pointer (*pa)[2]? Here the [2] is the last dimension of the array a[4][3][2]. I treat each "2-element array" as a box---a single unit. so that (*pa)[2] points a[4][3], each unit is an address of an array. Right?
Yes your understanding is correct...
Originally Posted by yifangt
I thought pa[4][3] should point to an address of the array{'q', 'r'}, but it actually points to the element, char "r". I lost the connection here (*pa)[2] vs. pa[4][3].
I'd suggest that you do those pointer arithmetic calculation using a pencil and paper...
char (*pa)[2] = &a[1][0];  /* original definition of pa */
pa points to -> {'g', 'h'}
pa[4] = *(pa + 4)  /* an array-and-index expression is equivalent to one written as a pointer and offset */
(pa + 4) points to -> {'o', 'p'}, and *(pa + 4) or pa[4] is that array via dereferencing

Since arrays are laid out sequentially...char 'r' is the 3rd element of the array "pa[4]" hence pa[4][3] == 'r'
Originally Posted by yifangt
Another confusion is the declaration of (*pa)[2] = &a[1][0] and (*ppa)[3][2] = &a[1]. The author is to emphasize the address of array and pointer. By array address only one anwer for char "r" which is a[2][2][1]. With pointer, you can have many ways(!?) What is the trick to connect two of them, say, if I declare (*pa)[2] = &a[2][0]?
If (*pa)[2] = &a[2][0] then you can get to the letter 'r'[0][5] or pa[2][1] or pa[1][3] or pa[3][-1]

Hope this helps...or have I confused you even more Smilie
For learning C I'd suggest sticking to "The C Programming Language" and "Expert C Programming"...those IMO are the 2 best books on the subject
This User Gave Thanks to shamrock For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #46
Difficulty: Easy
Regarding computer networking, the exact meaning of the term VPN is 'virtual protected network.'
True or False?

10 More Discussions You Might Find Interesting

1. Programming

How to Declare an array of function pointers?

I am attempting to create an array of function pointers. The examples I follow to do this are from: ... (3 Replies)
Discussion started by: spflanze
3 Replies

2. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

3. Programming

Traversing in Array of pointers

Please find the below program. the requirement and description of the program also given: ganesh@ubuntu:~/my_programs/c/letusc/chap9$ cat fa.c.old /* Program : write a program to count the number of 'e' in thefollowing array of pointers to strings: char *s = { "We will teach you how... (12 Replies)
Discussion started by: ramkrix
12 Replies

4. Programming

Problem with array of pointers

Hi All, I am using the array of pointers and storing the address of string.This is a global list. So i am using extern to give the reference of this list to another file and using reading the data from this string. But list is being corrupted and string is missing some characters in... (2 Replies)
Discussion started by: lovevijay03
2 Replies

5. Programming

Need help with the Pointers in C

I have a special character called ˘. When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = '˘'; char *fp; fp="XX˘"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

6. UNIX for Advanced & Expert Users

shared pointers

I am new to shared pointer conceot in C++ and hence require some clarification: For example: class A { public: virtual ~A() { } int x; }; typedef boost::shared_ptr<A>... (1 Reply)
Discussion started by: uunniixx
1 Replies

7. Programming

restricted pointers

Hi all. I am trying to use restricted pointers to allow the gcc compiler optimize the code, but I have not been able to make it work so far. I am testing with this code: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <sys/time.h> void vecmult(int n, int * restrict a, int... (0 Replies)
Discussion started by: carl.alv
0 Replies

8. Programming


Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue. void InsertFirst (tList *L, int val) { tElemPtr new; if((new = malloc(sizeof(tElemPtr))) == NULL) Error(); new->data = val; new->ptr = L->frst; L->frst = new;... (2 Replies)
Discussion started by: Milla
2 Replies

9. Programming


is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies

10. Programming

Pointers and array

hi all, let say i have a pointer exit, and this exit will store some value. how can i store the value that the pointer points to into an array and then print them out from the array. thanks in advance (2 Replies)
Discussion started by: dianazheng
2 Replies

Featured Tech Videos