Visit Our UNIX and Linux User Community


Pointers and array


 
Thread Tools Search this Thread
Top Forums Programming Pointers and array
# 8  
Old 09-16-2013
What bounds? Remember it's a big 1D array. Nothing stops you from going beyond the 'size'.

Also remember, 3 is TWO away from two. 0 isn't, 1 isn't, 2 is outside, 3 is outside.
# 9  
Old 09-16-2013
No, I did not get you.
As per the question is to use (*pa)[1] = &a[1][0]; (*ppa)[3][2] = &a[1] to reach "r", what is the correct answer although multiple addresses can print it out? Need more digestion. Can you suggest a good reference book about this? Thanks a lot!

Last edited by yifangt; 09-16-2013 at 07:37 PM..
# 10  
Old 09-24-2013
Quote:
Originally Posted by yifangt
No, I did not get you.
As per the question is to use (*pa)[1] = &a[1][0]; (*ppa)[3][2] = &a[1] to reach "r", what is the correct answer although multiple addresses can print it out? Need more digestion. Can you suggest a good reference book about this? Thanks a lot!
As Corona688 already said multidimensional arrays are nothing but a logical aggregation of 1D arrays. The data items are laid out sequentially so once you get that down it should be easy to visualize how they are combined logically to form 2 or 3 or n dimensional arrays. And the book I really like is "Expert C Programming" which has an entire chapter devoted to explaining multidimensional arrays along with lots of examples and pictures.
This User Gave Thanks to shamrock For This Post:
# 11  
Old 09-24-2013
Thanks Shamrock!

I was trying to catch the "dynamic" of the pointer in this example. The part that tricks me is(*pa)[1] = &a[1][0]; (*ppa)[3][2] = &a[1].
I want to confirm the structure of
(*pa)[1] and (*ppa)[3][2], which confused me with the assginment. &a[1][0] is the address for letter 'g',Is my understanding right? but what is &a[1]? I feel getting close to the point, but just not quite sure without a guide/reference/expert.
I read from the book saying the last dimension must always be given. I am not sure here for a[i][j][k], &a[1][0], i=1, j=0, k not assigned; and &a[1], i = 1, j, k not assigned. Or, (*pa)[1] is for k = 1; i, j not assigned here. Got totally lost. I wish the author had given explanation for this question.

Thanks!

Last edited by yifangt; 09-24-2013 at 03:00 PM..
# 12  
Old 09-24-2013
Quote:
&a[1][0] is the address for letter 'g',Is my understanding right?
Try printing its contents and see.
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 09-24-2013
Thanks Corona688!
I fully understand the full array is actually a big 1-dimension array from your previous reply.
I tried:
Code:
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]);

Code:
Output:
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.
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?
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].
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]?

Sorry I am so slow with this important point.

Last edited by yifangt; 09-24-2013 at 05:22 PM.. Reason: typo
# 14  
Old 09-24-2013
Quote:
Originally Posted by yifangt
Thanks Shamrock!

I was trying to catch the "dynamic" of the pointer in this example. The part that tricks me is(*pa)[1] = &a[1][0]; (*ppa)[3][2] = &a[1].[COLOR=black]

Going back to your first post you have...
Code:
char a[4][3][2] = {...};    /* a 3-dimensional array */
char (*pa)[2] = &a[1][0];   /* pa is a pointer to array of 2 chars */
char (*ppa)[3][2] = &a[1];  /* ppa is a pointer to a 2-dimensional array of chars */

So a[4][3][2] is made up of 4 items...each item is an array of 3 arrays...each of those 3 arrays is an array of 2 chars.
From this you can see that the element a[1] is an array of 3 arrays each made up of an array of 2 chars
Effectively saying that a[1] is a 2-d array of rank 3 x 2 and &a[1] is a pointer to that aka (*ppa)[3][2].
Quote:
Originally Posted by yifangt
I want to confirm the structure of (*pa)[1] and (*ppa)[3][2], which confused me with the assginment. &a[1][0] is the address for letter 'g',[COLOR=black]Is my understanding right?
&a[1][0] is a pointer to array of 2 chars...so it is a pointer to the array that contains 'g' and 'h'.
Quote:
Originally Posted by yifangt
but what is &a[1]? I feel getting close to the point, but just not quite sure without a guide/reference/expert.
As a[1] is an array of 3 arrays each made up of an array of 2 chars...&a[1] is a pointer to that aka (*ppa)[3][2]
Quote:
Originally Posted by yifangt
I read from the book saying the last dimension must always be given. I am not sure here for a[i][j][k], &a[1][0], i=1, j=0, k not assigned; and &a[1], i = 1, j, k not assigned. Or, (*pa)[1] is for k = 1; i, j not assigned here. Got totally lost. I wish the author had given explanation for this question.
Omg! which book have you been reading
Read "last dimension must always be given" to say that only the innermost dimension can be omitted while the rest need to be given so that the compiler can allocate storage for the array.
Moreover this is only true when initializing the array...if the array is defined without initialization all dimensions must be given...
This User Gave Thanks to shamrock For This Post:

Previous Thread | Next Thread
Test Your Knowledge in Computers #331
Difficulty: Medium
The last Windows operating system to be based on the Windows 9x kernel was Windows 98.
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: support.microsoft.com/en-us/help/30580/how-to-declare-an-array-of-pointers-to-functions-in-visual-c ... (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

pointers

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

pointers

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