could anyone tell me if the last lines of the following are true or false?
1)
int arrA[3] = {1,2,3};
int *ptrToarrA = arrA;
int *anotherptrToarrA = arrA[0];

2)
int arrB[2] = {1,2};
int *ptrToarrB = arrB;
int *anotherptrToarrB = *ptrToarrB;

Recommended Answers

All 4 Replies

Well, both are wrong. In the first case, you are trying to assign the first value of array A ie. 1 to the pointer. In the second case remove the '*' in *ptrToarrB; otherwise it is just the same mistake.

1)
int arrA[];
int *ptrToarrA = arrA;


2)
int arrB[];
int *ptrToarrB = arrB;
Now it is correct. When you enter a value in the array it store in *ptrToarrB and if you want to enter more values in the array you run a for or while loop for this purpose.
Any problem tell me.

Thank you so much guys!

1)
int arrA[];
int *ptrToarrA = arrA;


2)
int arrB[];
int *ptrToarrB = arrB;
Now it is correct. When you enter a value in the array it store in *ptrToarrB and if you want to enter more values in the array you run a for or while loop for this purpose.
Any problem tell me.

This is wrong, you don't assign memory for the array, this doesn't even compile, my compiler gives the following error:

t.cpp:7: error: storage size of `arrA' isn't known

To the OP (regarding post #1): Well, both are wrong for the same reason: Before you assign a value to a location in computer's memory, your pointer has to point to some memory first, otherwise you're overwriting random memory :)

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.