Visit Our UNIX and Linux User Community


Pointers and array


 
Thread Tools Search this Thread
Top Forums Programming Pointers and array
# 1  
Old 09-13-2013
Pointers and array

Hello, I read from a book exercise for a challenge. How to print out each letter of char array a[][][] by two different pointers pa and ppa in the example?
I have tried my code for letter "r" by testing without full understanding as only the first one worked.
Code:
#include<stdio.h>
int main()
{
    char a[4][3][2] = { {{'a', 'b'}, {'c', 'd',}, {'e', 'f'}},
                                   {{'g', 'h'}, {'i', 'j'}, {'k', 'l'}},
                                   {{'m', 'n'}, {'o', 'p'}, {'q', 'r'}},
                                   {{'s', 't'}, {'u', 'v'}, {'w', 'x'}}
                                 };

    char (*pa)[2] = &a[1][0];
    char (*ppa)[3][2] = &a[1];

//I tried out this one without much understanding.
    printf("The letter r can be reached by pointer *pa is: %c\n", pa[4][3]);
//No error at compiling time, but did not print out the letter r.
    printf("The letter r can be reached by pointer *ppa is: %c\n", ppa[4]);

    return 0;
}

Thank you in advance!
# 2  
Old 09-13-2013
This is a highly unusual way of using arrays and pointers.

Seeing where *pa starts may help:

Code:
printf("The first element of *pa is: %c\n", pa[0][0]);

The [1][0] has moved you one down the outermost dimension of the array, so it starts at {{'g', 'h'}, {'i', 'j'}, {'k', 'l'}}

[1][1] would move you one down and one in, so you'd start at {{'i', 'j'}, {'k', 'l'}}
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-14-2013
Interesting pointer address

Thanks Coron!

I know the author is to emphasize the relationship of pointer and array address. But I could not figure out the way of this complicate case. Could you please explain a little about these two lines, especially a[][] and a[]?
Code:
    char (*pa)[2] = &a[1][0];
    char (*ppa)[3][2] = &a[1];

And I have tried out:
Code:
printf("The first element of *pa is: %c\n", pa[0][0]); // g
printf("The first element of *pa is: %c\n", pa[1][0]); // h
printf("The first element of *pa is: %c\n", pa[0][1]); // i
printf("The first element of *pa is: %c\n", pa[1][1]); // j

How to get a ~ f ? and pa[0][2], p[1][0], ppa[0][0][2] all get letter "i" ???? Need more reading! Thank you again. Have a nice weekend.

---------- Post updated 09-14-13 at 01:56 AM ---------- Previous update was 09-13-13 at 06:02 PM ----------

Kind of get it now!
Because of the definition of (*pa)[2]=&a[1][0] and (*ppa)=&a[1], there is no way to get letter "a" ~ "f" unless I define:
Code:
    char (*pa)[2] = &a[0][0];
    char (*ppa)[3][2] = &a[0];

Wait a minute!!!.....Can anyone explain why there are multiple addresses to get the letter "r" ? I did an exhaust search, found out
Code:
pa[0][11], pa[1][9], pa[2][7], pa[3][5], pa[4][3], pa[5][1]

all give "r"; and also
Code:
ppa[0][0][11], ppa[0][1][9], ppa[0][2][7], ppa[0][3][5], ppa[0][4][3], ppa[0][5][1], ppa[1][0][5], ppa[1][1][3], ppa[1][2][1]

all give letter "r".
This must be something with pointer and array, and I have noticed the first 6 ppa[][][] addresses have the same index by ignoring the first dimension [0][][] . Two points I need experties:
1) multiple addresses to the same letter;
2) some index (11, 9, 7, 5) are way bigger than the max array dimension (which is 4, intuitively.).
Interesting, but something very new to me. Thanks a lot!

Last edited by yifangt; 09-14-2013 at 03:46 AM.. Reason: Wrong understanding of the question/answer.
# 4  
Old 09-16-2013
Quote:
Originally Posted by yifangt
Wait a minute!!!.....Can anyone explain why there are multiple addresses to get the letter "r" ?
It has to do with how memory is organized.

Your big array is one block of memory organized linearly, like

Code:
'a', 'b', 'c', 'd','e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x'

...and the only difference is the equation the compiler generates to access it.

If it's an n*2 array, the compiler does something like arr[(2*index2)+index1] to access the array. If you really wanted, you could ignore index2, and just step through the entire array with index1, but if you don't know what you're doing you might step beyond the bounds of the array by accident.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-16-2013
Thanks Corona!
I am getting close to the point now, except the two addresses:
Code:
ppa[1][1][3],  ppa[1][2][1]

which seem not out of the boundaries in the three dimension 4, 3 & 2.
# 6  
Old 09-16-2013
What is the question?
# 7  
Old 09-16-2013
My question is why both ppa[1][1][3], ppa[1][2][1] print out the same letter "r"?
ppa[1][2][1]is obvious, but ppa[1][1][3]seems to me not rational. Is it because [3] is out of the bounds again, which is supposed to be 2 as defined as ppa[3][2]? I am a little bit confused C automatic changes the address behind, somehow.
Thanks!

Last edited by yifangt; 09-16-2013 at 07:05 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #745
Difficulty: Medium
The Austrian Computer Kit (ACK) is a retargetable compiler suite and toolchain written by Andrew Tanenbaum.
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