-2

if

int number[];
int *tommy;

then whats the difference in the following statements? will both the statements not copy the address of number to tommy?

tommy = &number;

OR

tommy = number;

EDIT: sorry guys I meant int array rather than just int.

Firdous
  • 4,624
  • 13
  • 41
  • 80
  • 1
    The second one won't compile. – Joseph Mansfield Mar 22 '13 at 13:16
  • 3
    To learn basics like this, you should probably pick up a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Angew is no longer proud of SO Mar 22 '13 at 13:17
  • Everyone answering this question seems to have missed that `number` is an array. The second compiles, the first is a type error. – john Mar 22 '13 at 13:23
  • @john: That's because it wasn't an array until a minute ago. Now it's an array of unknown size, so it still won't compile. – Mike Seymour Mar 22 '13 at 13:24
  • Oh well, fundamental edits to the question without explanation. I very rarely do this but -1 from me. – john Mar 22 '13 at 13:24
  • @Firdous, I would ask this again, but this time ask what you really mean, and try to compile the code before you ask the question. – john Mar 22 '13 at 13:26
  • @john one might not have the dev environment anytime, the purpose of question is to grasp the concept, if you think question is below standards you are welcome to down vote! – Firdous Mar 22 '13 at 13:28
  • @Firdous, it's not really about the standard of the question, its just that because of the edit to the question, the answers will be extremely confusing to anyone subsequently reading this. Now you've acknowledged the edit I'm removed my down vote. Still think you might ask again though. – john Mar 22 '13 at 13:29
  • 1
    @Firdous There are several online compilers available, e.g. [ideone](http://ideone.com/). – Angew is no longer proud of SO Mar 22 '13 at 13:31

6 Answers6

2

The first one assigns the address of number into tommy. The second one tries to assign the number number into the pointer tommy, and it should fail to compile.

EDIT

With the edit, it's the other way round: the first one should not compile. An address of an int array is of type int (*)[size], which cannot be converted to int*.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
2

int number; defines an integer number. int *tommy; defines tommy to be a pointer pointing to an integer.

tommy = &number; Assigns the address of number to tommy. *tommy will give the actual number.

tommy = number stores the value of number in tommy. One, there should be a compilation error and on top of it, if you succeed to store number into tommy, then *tommy will try to deference from an invalid location whose address is same as the content of number and leads to segmentation fault.

Postscript

I am tempted to think if you saw an example as this

int a[10];
int *tommy;

tommy = a; // This will work

In this example, a is the name of the array and is equivalent to the address of array or the first element of the array i.e. &a[0]. Hence, storing a into tommy is valid.

Ganesh
  • 5,880
  • 2
  • 36
  • 54
  • thanks for interpreting rightly, so would tommy = a and tommy = &a be similar? – Firdous Mar 22 '13 at 13:30
  • Yes. `a`, the name of the array would be same as the address of the array `&a` and is also the address of the first element of the array `&a[0]` – Ganesh Mar 22 '13 at 13:32
1

For your example to compile, the array will need a size:

int number[1];

since you can't create a variable of unknown size.

tommy = &number;

error: cannot convert ‘int (*)[1]’ to ‘int*’ in assignment

This should not compile. &number is a pointer to an array, which is not convertible to a pointer to int, so cannot be assigned to a variable of that type.

tommy = number;

This is fine. When used in an expression, an array can be converted to a pointer to its first element. That pointer type is int*, the same as tommy, so the assignment is allowed.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

The first assignment is correct - it assigns the address of number to the pointer tommy.

The second assignment will assign the value of number to the pointer tommy.

suspectus
  • 16,548
  • 8
  • 49
  • 57
0

No, they won't, & is the reference operator or address of it will take the address of the variable you prea-append it with, so this does what you want:

tommy = &number

when you just use plain number it just return the integer value stored in number, which won't compile. This reference on pointers goes into a lot more details.

Update:

So after the edit to the question, this will not compile without a size:

int number[];

this is valid:

int number[10];

So now it is reversed, this is valid and will compile:

tommy = number ;

The second line attempts to treat an array of integers as a single integer and the compiler will not allow this and so it is not valid:

tommey = &number ;
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0

No, the two are fundamentally different. The expression &number evaluates to the address at which the value of number is stored, whereas the expression number evaluates directly to the value itself.

Suppose that the value of number is 42, and that it is stored att adress 1024 in memory. Memory is linear*, so it will look something like this:

+--------+ Address 0
| ...... |
+--------+
|   42   | Address 1024 (value is stored here)
+--------+
| ...... |
+--------+ Address xxxx

Now, the statement tommy = &number; will assign the value 1024 to tommy, since &number == 1024, the address of number.

The other statement, provided it compiles at all, will assign the (nonsensical) value 42 (the value of number) to tommy. Any attempts to dereference (access the data pointed to by) tommy are likely to generate a segmentation fault, because 42 is (probably) not a valid address.

*At least, the operating system will give you the illusion that your address space is linear and contiguous.

Martin Törnwall
  • 9,299
  • 2
  • 28
  • 35