0

I have a question. I created a correctly initialized integer pointer int * p and a correctly initialized integer array int * array1 = new int[]

Which of the following is legal code?

p = array1;

array1 = p;

Or are both correct?

is this possible as well p[0] since, to my pointer arithmetic knowledge, it doesn't add anything.

All in c++

1
  • its in c++ sorry i didn't mention it before.
    – Jonathan
    Mar 17, 2011 at 4:49

8 Answers 8

1

If the question is trying to get at pointers versus arrays, they are not always compatible. This is hidden in the presented code because the array is immediately converted to a pointer.

int*  array1   = new int[5]; // Legal, initialising pointer with heap allocated array
int array2[5] = {0}; // Declaring array directly on the stack and initalising with zeros
int *p = 0; // Declaring pointer and initialising to numm
p = array2; // Legal, assigning array to pointer
p = array1; // Legal, assigning pointer to pointer
array1 = p; // Legal, assigning pointer to pointer
array2 = p; // ILLEGAL, assigning pointer to array

Here array2 has an array type and cannot be used to store a pointer. Actually, the array cannot be reassigned at all, as it is not an l-value.

int array3[5] = {0}; // Declaring array directly on the stack and initalising with zeroes
array3 = array2; // ILLEGAL, array not an l-value

The array has a fixed address and reassiging it would be similar to trying to write:

int i = 0;
&i = p;

[Hopefully, trying to reassign the location of a variable is obvious nonsense.]

0

Both are legal.

The first, "p = array1", will cause your correctly initialized integer pointer to be leaked and to point p to the first occurrence of the array that array1 points to.

The second, "array1 = p", will cause the correctly initialized integer array to to be leaked and to point array1 to the single int that p points to.

So I suspect I'm missing something. Could you perhaps post complete code?

1
  • Sorry, I do not have any code for this because its just a question I stumbled upon. I want to know if it is possible to assign a dynamic array to a pointer and vice versa.
    – Jonathan
    Mar 17, 2011 at 4:54
0

If both p and array1 are declared as "int *", as you imply here, then either assignment is legal, by definition.

0

both are leagal. passing adresses to eachother. dont knw what u want to do

6
  • This is really a comment, not an answer to the question. Please use "add comment" to leave feedback for the author. Aug 15, 2012 at 15:09
  • Geez u mentioning me this after 1 year and 5 months. Ridiculos Aug 17, 2012 at 6:20
  • How is it ridiculous? It came up through the new review system. Aug 17, 2012 at 7:56
  • how abt the other comments posted as answer, wy targetting me only?? its ridiculous though, ur pin poiniting the issue after one year and 5 months... nice. too late i guess. Aug 23, 2012 at 8:16
  • It came up in the new reviewing system, so blame that for 'targeting you only'. Aug 23, 2012 at 9:17
0

According to the explanation provided in this site. The first assigned in is correct but the second assignment cannot be considered valid. Please look under the title 'Pointers and arrays'.

1
  • See my answer as to why the second assignment is valid and is actually not the same as that in the link.
    – Keith
    Mar 17, 2011 at 5:21
0

You can assign your array variable to pointer variable. So p = array1 is correct.

You can refer this code.

#include <iostream>
using namespace std;

int main ()
{
  int numbers[5];
  int * p;
  p = numbers;  *p = 10;
  p++;  *p = 20;
  p = &numbers[2];  *p = 30;
  p = numbers + 3;  *p = 40;
  p = numbers;  *(p+4) = 50;
  for (int n=0; n<5; n++)
    cout << numbers[n] << ", ";

  for (int n=0; n<5; n++)
    cout << p[n] << ", ";
  return 0;
}

If both are pointers then no need to declare pointer as array like int *array1 = new int[].

0

Both variables are of type pointer to int. So both assignments are legal. The fact that one of the variables is an array doesn't matter in C. Arrays and pointers are the same after initialization.

0

"I created a correctly initialized integer pointer int * p and a correctly initialized integer array int * array1 = new int[]"

First of all, keeping the syntactical mistakes aside, the terminology is wrong.

  1. int *p; - Here you are just declaring a pointer to hold an integer variable's address. You are not initializing it. Declaration is different from initialization and initialization is different from assignment.
  2. int *array1 = new int[]; - Keeping the error aside, this is not an initialized integer array. array1 is a pointer pointing to an array of integers. And passing nothing to [] is incorrect. A value needs to be passed that decides number of memory locations to be allocated for holding integer values.

Answering the questions.

Which of the following is legal code?

  1. p = array1;

    If, array1 is properly initialized it's correct. p points to the first integer in the array1. Even if it is not properly initialized also, it is correct. In this case, both array and p points to garbage values.

    int* array1 = new int[5]; // Notice **5** being passed.

  2. array1 = p;

    Infact this operation is useless. Both p and array1 were both pointing to the same location earlier. But this is legally correct.

  3. is this possible as well p[0] ?

    Yes, it's correct. p can dereference to 0 to 4 as it's index values. If you just want to p[0], in that case -

    int* array1 = new int ; // Notice nothing being passed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.