Ok, The only reason I'm asking this is because I want to be absolutely clear on it. In C++, if I write

int* ipSomePointer;

Is this the same thing as:

int *ipSomePointer;

or

int& iSomeRef = ...;
int &iSomeRef = ...;

It does give the same result as each other but there could be some unknown thing that a simple test would not show me, I just want to be clear.

EDIT: I also have another question, if I have this code:

int** toipArray= &ipArray;

where ipArray will point to an array of ints.
What would each of these memory addresses refer to:

cout<<toipArray<<endl;
      cout<<*toipArray<<endl;

Does the toipArray refer to it's own address? and the *toipArray refers to the memory address that it points to(i.e. &ipArray)?

Recommended Answers

All 10 Replies

Member Avatar for JSPMA1988

Yes, it's the same thing. I prefer to have the pointer and the reference operator immediately preceding the variable, but that's pure personal taste.

Its the same for both examples.

sorry, you maybe didn't see my edit, but would you be able to answer my other question? Thanks alot.

For the second one. Remember, toipArray can have its own address. What it points to is key.
And in your case it points to an array of ints. If that didn't answer your question, then please re-phrase it. I wasn't able to read it clearly.

Try looking at the attached code...It may help straighten things out or it just might confuse you more..

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i = 0;
	int *iptr = (int*)malloc(sizeof(int) * 10);
	int **tptr;

	for (i = 0; i < 10; ++i)
		*(iptr + i) = i;

	for (i = 0; i < 10; ++i)
		fprintf(stdout, "ans->%d\n", *(iptr + i));

	tptr = &iptr;

	for (i = 0; i < 10; ++i)
		fprintf(stdout, "ans->%d\n", *(*tptr + i));
	
	return 0;
}

I really should have used C++, so here look at this one

#include <iostream>

int main()
{
	int i = 0;
	int *iptr = new int[10];
	int **tptr;

	for (i = 0; i < 10; ++i)
		*(iptr + i) = i;

	for (i = 0; i < 10; ++i)
		std::cout << "ans->" << *(iptr + i) << std::endl;

	tptr = &iptr;

	for (i = 0; i < 10; ++i)
		std::cout << "ans->" << *(*tptr + i) << std::endl;
	
	delete [] iptr;
	
	return 0;
}

sorry, you maybe didn't see my edit, but would you be able to answer my other question? Thanks alot.

That's why you souldn't edit posts except to correct mistakes...

>>std::cout << "ans->" << *(iptr + i) << std::endl;
>>std::cout << "ans->" << *(*tptr + i) << std::endl;

Just for clarity, it would be better to use array notation:

std::cout << "ans->" << iptr[i] << std::endl;
std::cout << "ans->" << tptr[0][i] << std::endl;

Hi all, thanks for the replies, I wrote this piece of code to check what if I was on the right track(don't know why I didn't do it in the first place). I only wanted to check the address at a certain point in the array and change it just so I could see it was actually different.

# include <iostream>
	using namespace std;

int main () {
	int iaArray[] = {1,2,3,4,5};
	int &iaRef = iaArray[4];
	cout<<"value of iaRef: "<<iaRef<<endl;
	int *ipArray=0;
	int** toipArray= &ipArray;
	ipArray = &(iaArray[0]);
	cout<<"value at iaArray[0]: "<<iaArray[0]<<endl; 
	cout<<"value at iaArray[0] using dereferencing of pointer: "<<*ipArray<<endl; 

	ipArray = &(iaArray[3]);//redeclare pointer, is this legal?

	cout<<"value at iaArray[3] using dereferencing of pointer: "<<*ipArray<<endl; 
	cout<<"the address in memory of iaArray[3] "<<&iaArray[3]<<endl; 
	cout<<"the address in memory of toipArray: "<<toipArray<<endl; 
	cout<<"the address in memory where toipArray points(&iaArray)"<<*toipArray<<endl;
	cout<<"the de-referenced pointer2pointer"<<**toipArray<<endl;
	
	
	return 0;
}

Are all my statements here true? Also, is it good practice to reassign pointers?

Its very common to reassign pointers... So your line 14 is valid.

Ok thanks all. Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.