Jump to content

Arrays and Pointers

RexLee

1.How to use *p1[10] and (*p1)[10] and how are they useful?

2.How is this different const int *a and const int const *a?

3.Why do we need pointers to pointers?

Link to comment
Share on other sites

Link to post
Share on other sites

I'm assuming you are talking about C/C++ and languages using similar implementations

of pointers.

  • Is a question of operator precedence. *p1[10] will first take the 11th element in

    the array p1 and then dereference that, thereby selecting whatever p1[10] is pointing

    to. (*p1)[10] will first dereference p1 and then select the 11th element of whatever

    p1 itself is pointing to.

  • EDIT: Ooops, I overlooked two const in that question :o. See @WanderingFool's answer

    below. I'll leave my original answer here for posterity. /EDIT int *a is a simple pointer to an int.

    int const *a is a pointer whose target location has been defined as constant and therefore

    cannot be changed via dereferencing a. You can however still change a's target if you access

    it via another way (see example).

  • Without pointers to pointers we cannot have arrays of pointers (an array's identifier

    is basically just a constant pointer to its first element).

Example code (I have compiled and run this on gcc):

EDIT: AAARGH, the forum deleted my code when I was editing my post. I'll see if

I can recreate it. :angry:/EDIT 

If anybody spots an error please let me know.

 

#include <stdio.h>int main(){	int p1[3];	p1[0]=1;	p1[1]=2;	p1[2]=3;	int *p2[3];	p2[0]=&p1[0];	p2[1]=&p1[1];	p2[2]=&p1[2];        /* Will print 1,1,3 */	printf("%d\n%d\n%d\n",p1[0],*p2[0],(*p2)[2]);        /* These three will all print out the same number, which is           the memory address of p1's first element. */	printf("%d\n",&p1[0]);	printf("%d\n",p1);	printf("%d\n",*p2);	        /* These two will print out the memory address of p2's first element */	printf("%d\n",&p2[0]);	printf("%d\n",p2);	int const *a=p2[0];	printf("%d\n",*a);        /* Uncommenting this should throw a compilation error. */        /* a's target location cannot be changed via dereferencing a. */	/*	*a=5;	printf("%d\n",*a);	*/        /* Whereas this will work and print out 5 because we are not accessing           a's target location via dereferencing a. */	*p2[0]=5;	printf("%d\n",*a);	return 0;}

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

What alphenwasser said.  Although I would like to say a few things.

 

1.Q:How to use *p1[10] and (*p1)[10] and how are they useful?

A: The question all depends on how *p1[10] and (*p1)[10] are used.  There are actually two ways to interpret this:

1) int *p1[10]  //See Alphenwasser, his was pretty good2) int a = *p1[10] //Also see Alphenwasser, but I will address the reading of these variables below (in 2) and the usefulness in 3

2.Q:How is this different const int *a and const int const *a?

A: hmm, I would have thought "const int const *a" would throw an errors...anyways an important rule of thumb I learned is start at the variable, and read right until you hit a ), ; or = then read left.  So like this:

const int *a becomesa is a pointer (*) to an integer (int) constant (const)An integer constant being the same as a constant integer, so the pointer can be changed but the integer cannotint const *aa is a pointer to a constant integerSame as aboveint * const a;a is a constant pointer to an integerSo the pointer cannot be changed but the integer value can be changed.const int const *aa is a pointer to a constant integer constant....This is why I think it would be a compiler error, either way int would be constant and pointer should be changeable.const int * const a[10];a is an array of 10 (hit ; so starting back at "a" reading left) constant pointers to a const int;int a = *p1[10]; //Could interchange with p1[10][0];get the 10th element of p1 and dereference it to an integerint a = (*p1)[10]; //Could interchange with int a = p1[0][10];Dereference p1 and get then the 10th elementint const * const aa is a constant pointer to a constant integerNothing can be changed

3.Q:Why do we need pointers to pointers?

A: Pointers to pointers can come in handy when you want to creating an array of arrays, with different sizes.  Say you wanted an user to input 30 lines with 10 characters each, this would be easy char str[30][10].  But now lets say you relax the restrictions, so the user could input 10 characters each but any amount of lines, you could get away with char str[][10]...still good, but what happens when you relax it so the user can input any number of characters and any number of lines.

char str[][] would become invalid, so pointers should be used.  char *str[] is great though....but to make it even more general, if you want to manipulate the incoming input you have to copy the array first...so using malloc you get a pointer, so in essence it becomes char** str

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

WanderingFool, on 05 Jul 2013 - 10:21 PM, said:

2.Q:How is this different const int *a and const int const *a?

Aaargh, I misread that question, missed a few const... :rolleyes:

Sorry about that.

EDIT:

2.Q:How is this different const int *a and const int const *a?

A: hmm, I would have thought "const int const *a" would throw an errors...anyways an important rule of thumb I learned is start at the variable, and read right until you hit a ), ; or = then read left.  So like this:

const int *a becomesa is a pointer (*) to an integer (int) constant (const)An integer constant being the same as a constant integer, so the pointer can be changed but the integer cannotint const *aa is a pointer to a constant integerSame as aboveint * const a;a is a constant pointer to an integerSo the pointer cannot be changed but the integer value can be changed.const int const *aa is a pointer to a constant integer constant....This is why I think it would be a compiler error, either way int would be constant and pointer should be changeable.const int * const a[10];a is an array of 10 (hit ; so starting back at "a" reading left) constant pointers to a const int;int a = *p1[10]; //Could interchange with p1[10][0];get the 10th element of p1 and dereference it to an integerint a = (*p1)[10]; //Could interchange with int a = p1[0][10];Dereference p1 and get then the 10th elementint const * const aa is a constant pointer to a constant integerNothing can be changed

I have quickly tried this:

const int const *a
And on gcc it does not throw a compilation error. However I have not yet been able to

figure out what exactly differentiates this construct. I cannot modify a's target by

dereferencing a, but I can change a to a different memory location.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Aaargh, I misread that question, missed a few const... :rolleyes:

Sorry about that.

EDIT:

I have quickly tried this:

const int const *a
And on gcc it does not throw a compilation error. However I have not yet been able to

figure out what exactly differentiates this construct. I cannot modify a's target by

dereferencing a, but I can change a to a different memory location.

 

 

Ah, good to know it actually compiles...In theory it should do nothing, it is just a redundant statement since it is a pointer to a constant integer constant :P

0b10111010 10101101 11110000 00001101

Link to comment
Share on other sites

Link to post
Share on other sites

Ah, good to know it actually compiles...In theory it should do nothing, it is just a redundant statement since it is a pointer to a constant integer constant :P

Yeah that would fit with my results. I have been getting the same behavior between

these two statements.

If I enable the --pedantic-error in gcc it does actually refuse to compile and

complains about the "duplicate const" though.

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×