473,388 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

address of an array

65
int myArray[10];

it is said that array names are address information.
what i want to ask is
does myArray mean exactly the same thing with &myArray [0]
if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

1 -what is the relation and difference between myArray and &myArray [0]
2- what is the relation and difference between (*myArray ) and myArray [0]


thanks in advance...
May 23 '07 #1
2 2385
JosAH
11,448 Expert 8TB
int myArray[10];

it is said that array names are address information.
what i want to ask is
does myArray mean exactly the same thing with &myArray [0]
if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

1 -what is the relation and difference between myArray and &myArray [0]
2- what is the relation and difference between (*myArray ) and myArray [0]


thanks in advance...
In C (and C++) the name of the array boils down to the address of the first
element of that array. The sizeof() operator is a bit funny, i.e. it is evaluated
at compile time: when you do 'sizeof(array)' the compiler knows what you're
talking about, i.e. the size in bytes taken by the entire array. OTOH when you
do this: 'sizeof(&array[0])' all the compiler knows is that you've taken the
address of the first element of that array, which is a simple pointer. So the
result of both sizeof() examples will most likely be different: the size of your
array measured in bytes versus the size of a simple pointer.

kind regards,

Jos
May 23 '07 #2
AdrianH
1,251 Expert 1GB
int myArray[10];

it is said that array names are address information.
what i want to ask is
does myArray mean exactly the same thing with &myArray [0]
if so, why sizeof(myArray ) is not equal to sizeof(&myArray [0]).

1 -what is the relation and difference between myArray and &myArray [0]
2- what is the relation and difference between (*myArray ) and myArray [0]


thanks in advance...
An array can degrad to a pointer, but it is not one.

If you pass an array to something that takes a pointer (of the same type as in the array) it will degrade into a pointer. At that point a sizeof will show the size of the pointernot of the array (because it is checked at compile time, not runtime). Example:
Expand|Select|Wrap|Line Numbers
  1. void f(int *array)
  2. {
  3.   cout << "second element = " << *(array+1) << endl;
  4.   cout << "second element = " << array[1] << endl;
  5.   ++array;
  6.   cout << "second element = " << *array << endl;
  7.   cout << "second element = " << array[0] << endl;
  8. }
  9.  
  10. int main()
  11. {
  12.   int array[4] = { 1,2,3,4 };
  13.   f(array);
  14. }
  15.  
If you pass an array to something that looks like it is supposed to take an array using the empty brackets ('[]'), beware. This is actually considered a pointer, and a non const one at that. It is for readability only. The sizeof operator works the same way as if it were a pointer. Example:
Expand|Select|Wrap|Line Numbers
  1. void f(int array[])
  2. {
  3.   cout << "second element = " << *(array+1) << endl;
  4.   cout << "second element = " << array[1] << endl;
  5.   ++array;
  6.   cout << "second element = " << *array << endl;
  7.   cout << "second element = " << array[0] << endl;
  8. }
  9.  
  10. int main()
  11. {
  12.   int array[4] = { 1,2,3,4 };
  13.   f(array);
  14. }
  15.  
If you pass an array to something that takes a reference to an array, it needs to be the same size.
Expand|Select|Wrap|Line Numbers
  1. void f(int (&array)[4])
  2. {
  3.   cout << "second element = " << *(array+1) << endl;
  4.   cout << "second element = " << array[1] << endl;
  5.   // ++array;  // This is invalid
  6. }
  7.  
  8. int main()
  9. {
  10.   int array[4] = { 1,2,3,4 };
  11.   f(array);
  12. }
  13.  

Adrian
May 23 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Timo | last post by:
I am trying to get address of myStruct to a string array texts. I am using M$ Visual C++ 6.0 (this is not OS specific question, though, this code should also work on 16 bit embedded compiler ;)). ...
9
by: Luke Wu | last post by:
Hello, I'm having some problems understanding 2 dimensional arrays. My problem relates to the following code: #include <stdio.h> #define M 3 #define N 3
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
0
by: comp.lang.php | last post by:
I wrote a method that should check if an email address is valid. In another method I've already checked to see if $_POST exists and is well-formed, so those checks are not necessary in this scope....
10
by: | last post by:
If I have an array of int: int array; I suppose the correct way to clear it using memset() would be: memset(array, 0, 8 * sizeof(int)); However, I've seen the following in a piece of code:...
8
by: =?big5?B?r0W84Q==?= | last post by:
Hi All C gurus: Below is a small program to print out the address of array and address of array variable: int main() { char array = "haha"; printf("array:%x\n", array); printf("&array:%x\n",...
36
by: Julienne Walker | last post by:
Ignoring implementation details and strictly following the C99 standard in terms of semantics, is there anything fundamentally flawed with describing the use of a (non-inline) function as an...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.