| | |
Little Simple array question :)
Thread Solved |
>I just was wondering why the array its self are a pointer to the first element?
Because it was a design decision by the language creator.
>and what is the difference between an array and a pointer?
An array is a collection of objects, and a pointer is a variable whose value is an address. A pointer can of course point to any item in an array, or to the array itself. In value context, an array name is converted to a pointer to the first element. In object context, an array name refers to the array as a whole. The three object contexts are:
1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer
Every other use of an array is in value context.
Because it was a design decision by the language creator.
>and what is the difference between an array and a pointer?
An array is a collection of objects, and a pointer is a variable whose value is an address. A pointer can of course point to any item in an array, or to the array itself. In value context, an array name is converted to a pointer to the first element. In object context, an array name refers to the array as a whole. The three object contexts are:
1) As the operand to sizeof
2) As the operand to the address-of operator
3) As a string constant initializer
Every other use of an array is in value context.
I'm here to prove you wrong.
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
So when we declare an array here:
int arr[2] = {2,3};
we are taking 8 bytes of memory , then we are giving to 4 bytes of it the value 2 and the next 4 bytes of it the value 3.
then "arr" is converted to a pointer to the first element.
1.i got it so far, is it right??
2.What about "arr" location in memory i was checking its address and its address is the same as the address it is pointing to, so its means that "arr" is the first element and its points to its self?
int arr[2] = {2,3};
we are taking 8 bytes of memory , then we are giving to 4 bytes of it the value 2 and the next 4 bytes of it the value 3.
then "arr" is converted to a pointer to the first element.
1.i got it so far, is it right??
2.What about "arr" location in memory i was checking its address and its address is the same as the address it is pointing to, so its means that "arr" is the first element and its points to its self?
>we are taking 8 bytes of memory , then we are giving to 4 bytes of it the
>value 2 and the next 4 bytes of it the value 3.
No. An int doesn't have to be 4 bytes. The statement is otherwise correct, but I would word it like this: Enough memory is set aside for two integers. The first integer is given the value 2 and the second integer is given the value 3.
>then "arr" is converted to a pointer to the first element.
No, the array name is only converted to a pointer in value context. That is, when you try to use it. This is a declaration, not a use. If you try to print the first item, that's value context and the array name is converted to a pointer:
>What about "arr" location in memory
All of the following can and often do produce the same address:
This doesn't mean they're the same thing though. There's a difference in type between the address of the first element and the address of the array. The address of the first element iis a pointer to int and the address of the array is a pointer to an array of int. There's no "pointing to itself" going on.
>value 2 and the next 4 bytes of it the value 3.
No. An int doesn't have to be 4 bytes. The statement is otherwise correct, but I would word it like this: Enough memory is set aside for two integers. The first integer is given the value 2 and the second integer is given the value 3.
>then "arr" is converted to a pointer to the first element.
No, the array name is only converted to a pointer in value context. That is, when you try to use it. This is a declaration, not a use. If you try to print the first item, that's value context and the array name is converted to a pointer:
C Syntax (Toggle Plain Text)
/* arr is not converted to a pointer */ int arr[2] = {2, 3}; /* arr is converted to a pointer */ printf ( "%d\n", arr[0] );
All of the following can and often do produce the same address:
C Syntax (Toggle Plain Text)
int arr[2] = {2, 3}; /* Address of the first element */ printf ( "%p\n", (void*)&arr[0] ); /* Address of the array converted to a pointer */ printf ( "%p\n", (void*)arr ); /* Address of the array as an object */ printf ( "%p\n", (void*)&arr );
I'm here to prove you wrong.
•
•
•
•
I just was wondering why the array its self are a pointer to the first element?
and what is the difference between an array and a pointer?
first, i want you to read a definition of arrays,
An array is an example of a homogeneous random access data structure. An array is merely a collection of similar data elements such as integers, floats, characters, etc. which is stored together with a common name, and addressed by means of index that tells the location of the particular data entity in the array..
When we use homogeneous to an array, it means that each cells have the same data type, and the random-access aspect tells that we can access any of the cells of an array directly by just using its indices.
for example, given is an array a[50], (note that an array begins in 0) in a[20] we assigned a value of 10 and in a[2] we assigned a value of 30. we can make use of this values simply by using the array's indices, for example,
a[20] + a[2] is similar to 10 + 30..
now, we can relate pointers to arrays..we can say that any cell of an array can be a pointer. an array can hold data value and its address..
good luck!
•
•
Join Date: May 2007
Posts: 69
Reputation:
Solved Threads: 1
I cant understand this
/* Address of the first element */
printf ( "%p\n", (void*)&arr[0] );
/* Address of the array converted to a pointer */
printf ( "%p\n", (void*)arr );
/* Address of the array as an object */
printf ( "%p\n", (void*)&arr );
If the address of the array is the same as its value, why?
I did the same with cout look
int arr[2] = {2,3};
cout << arr << endl << &arr;
The address of arr is the value of arr , why is that?
/* Address of the first element */
printf ( "%p\n", (void*)&arr[0] );
/* Address of the array converted to a pointer */
printf ( "%p\n", (void*)arr );
/* Address of the array as an object */
printf ( "%p\n", (void*)&arr );
If the address of the array is the same as its value, why?
I did the same with cout look
int arr[2] = {2,3};
cout << arr << endl << &arr;
The address of arr is the value of arr , why is that?
Last edited by laconstantine; May 10th, 2007 at 7:16 am.
>I cant understand this
What a surprise.
>If the address of the array is the same as its value, why?
The address of the array is the memory location that the array starts at, right? The address of the first element of the array is also the memory location that the array starts at. Why shouldn't they be the same? The only difference is the resulting data type.
What a surprise.
>If the address of the array is the same as its value, why?
The address of the array is the memory location that the array starts at, right? The address of the first element of the array is also the memory location that the array starts at. Why shouldn't they be the same? The only difference is the resulting data type.
I'm here to prove you wrong.
>So the first element and the array its self are at same memory location
Not necessarily. There's no guarantee that the address of the array as a whole is the same as the address of the first element. That's usually the case though.
>and when i use the array name the compiler treats its like a pointer , but
>its not a pointer right?
No, it is a pointer. When you use the array name in value context, the compiler actually converts it to a pointer to the first element. This is only the array name though, the array itself is never a pointer.
Not necessarily. There's no guarantee that the address of the array as a whole is the same as the address of the first element. That's usually the case though.
>and when i use the array name the compiler treats its like a pointer , but
>its not a pointer right?
No, it is a pointer. When you use the array name in value context, the compiler actually converts it to a pointer to the first element. This is only the array name though, the array itself is never a pointer.
I'm here to prove you wrong.
![]() |
Similar Threads
- Simple array question (C++)
- Simple 2d array help (Visual Basic 4 / 5 / 6)
Other Threads in the C Forum
- Previous Thread: need source code for BOOTH'S mul algorithm
- Next Thread: help me
| Thread Tools | Search this Thread |
#include adobe ansi api array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






