why is this a false statement?

Because the name of an array....

Page 1 of 1

7 Replies - 498 Views - Last Post: 29 June 2009 - 09:28 PM Rate Topic: -----

#1 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

why is this a false statement?

Post icon  Posted 29 June 2009 - 08:48 PM

Because the name of an array is a pointer to the first element of the array, array names can be manipulated in precisely the same manner as pointers.
Is This A Good Question/Topic? 0
  • +

Replies To: why is this a false statement?

#2 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: why is this a false statement?

Posted 29 June 2009 - 08:50 PM

It sounds true to me. An array name is also a pointer

This post has been edited by Zerobu: 29 June 2009 - 08:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: why is this a false statement?

Posted 29 June 2009 - 08:51 PM

1-please do not ask the question in the description. put that in the body

2-as Zerobu has stated, yes an array name is also a pointer because it has a list of variables and you use i as the index to the location of a certain number in the list. :)

This post has been edited by computerfox: 29 June 2009 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

Re: why is this a false statement?

Posted 29 June 2009 - 08:59 PM

View PostZerobu, on 29 Jun, 2009 - 07:50 PM, said:

It sounds true to me. An array name is also a pointer


my friend says its false because arrays must be a valid value, whereas a pointer can be written as a math expression.
does that sound right i couldnt find in my book where they talk about the differences
Was This Post Helpful? 0
  • +
  • -

#5 Zerobu  Icon User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Re: why is this a false statement?

Posted 29 June 2009 - 09:04 PM

Your friend is only half right, an array name is also a pointer that can also be used to do pointer arithmetic to operate on that array.


Perhaps this link will help.
http://www.fredosaur...aspointers.html

This post has been edited by Zerobu: 29 June 2009 - 09:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 angelita_azul  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 28
  • Joined: 13-June 09

Re: why is this a false statement?

Posted 29 June 2009 - 09:11 PM

View PostZerobu, on 29 Jun, 2009 - 08:04 PM, said:

Your friend is only half right, an array name is also a pointer that can also be used to do pointer arithmetic to operate on that array.


Perhaps this link will help.
http://www.fredosaur...aspointers.html

ah ha! i found it the book says array names cannot be modified in arithmetic expressions because array names are constant pointers. yay
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: why is this a false statement?

Posted 29 June 2009 - 09:15 PM

Array names and pointers are closely linked but they are not the same thing.

you can assign a value to a pointer, you can't assign a value to an array name. (so a pointer can be a lvalue, but an array name can only be an rvalue).

The operator sizeof will give different results for array names and pointers (for the array name it gives the size of the entire block of memory used by the array, for the pointer it gives the size of the pointer.

there is this: &array == array but normally &ptr != ptr (though it is of course possible to assign a pointer to address itself).

I am pretty sure there are at least 5 other differences (although this is all I could think of at the moment).
Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: why is this a false statement?

Posted 29 June 2009 - 09:28 PM

No one got it right here (EDIT: except for NickDMax). That's OK, arrays and pointers and the equivalence is confusing. Plus books tend to get it wrong too...

Quote

Because the name of an array is a pointer to the first element of the array
The name is the identifier. Syntactically, the identifier stands for an array. It does not stand for a pointer.

Quote

array names can be manipulated in precisely the same manner as pointers.
They cannot.

#include <iostream>
#include <typeinfo>

int main()
{
	int anArray[10];
	int *aPointer = &anArray[0];
	//aPointer = &anArray[0]; //Are aPointer and anArray the same exact thing?
	std::cout << typeid(anArray).name() << " is the same as " << typeid(aPointer).name() << "?\n";
	// aPointer = 0;
	// anArray = 0;
	// so, if you can do the same operation on pointers and arrays, then uncomment both lines and try to compile...
}



The problem is that even though you can have an array, when you refer to it, syntactically, it is decayed to a pointer. But you have to be careful, because certain operations are not valid. Unless you pass an array in a function. Because of the syntactical decay, it's like passing a pointer, so the function actually takes in a pointer. But the decay only happens on the first dimension of the array, which is why if you have a multidimensional array, you can omit the first dimension, but you must indicate the following dimensions...

It's confusing, until you go through the rules properly. For a basic introduction, start with the C FAQ.

This post has been edited by Oler1s: 29 June 2009 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1