开发者

I don't understand one dimensional array handling with function passing

开发者 https://www.devze.com 2023-01-07 22:07 出处:网络
I have an array named record[10] whose type is a table structure, say { int, int, long, long,char} I have a function to which I want to pass the address of this array which gets called in a loop:

I have an array named record[10] whose type is a table structure, say { int, int, long, long,char}

I have a function to which I want to pass the address of this array which gets called in a loop:

for(i = 0 ; i<10; i++)
{
    // internal resolution will be *(record + i) will fetch an address
    function(record[i]);
}

I'm confused as to why it is not working. I know it is related to basics.

It star开发者_如何学Cted working with

for(i = 0 ; i<10; i++)
{
    // then why do I need to pass this address of address here 
    function(&record[i]); 
}


*(record + i) is not in fact an address. record is an address, and so is (record + i), but *(record + i) is the value stored at the address represented by (record + i). Therefore, calling function(record[i]) is the same as function(*(record + i)), which will pass the value of the array element to the function, not a pointer.

The syntax &record[i] is not taking the address of an address. It is taking the address of record[i], which is an object. The braces have a higher precedence than the ampersand, so &record[i] is equivalent to &(record[i]). You can think of it as expanding to &(*(record + i)) and then simplifying to (record + i).

Update: To address your question from the comment, an array "decays" into a pointer if you reference the name of the array by itself. If you add square brackets [], you will get a value from within the array. So, for your example, say you have an array of structures:

struct A {
    ...
    char abc[10];
    ...
} record[10];

Then, you would have:

  • record[i] - an object of type struct A from the record array
  • record[i].abc - the abc array inside a particular record object, decayed to a pointer
  • record[i].abc[k] - a specific character from the string
  • &record[i].abc[0] - one way of creating a pointer to the string

The notation record[i]->abc that you mention in your comment cannot be used, since record[i] is an object and not a pointer.

Update 2: In regards to your second comment, the same rules described above apply regardless of how you nest the array within a structure (and whether you access that structure directly or through a pointer). Accessing an array using arrayname[index] notation will give you an item from the array. Accessing an array using arrayname notation (that is, using the array name by itself) will give you a pointer to the first element in the array. If you need more details regarding this phenomenon, here are a couple of links that explain arrays and the way that their names can decay into pointers:

  • http://boredzo.org/pointers/
  • http://www.ibiblio.org/pub/languages/fortran/append-c.html
  • http://c-faq.com/aryptr/index.html


You're saying two different things in your question. First, you say you want to pass the address of the array, but in the code you appear to be trying to pass the address of a particular element. One of the features of C is that an array will automatically turn into pointer to the array's first element when you use it in certain contexts. That means these two calls are 100% equivalent:

function(array);
function(&array);

To get the address of a particular array element, you can do two things. One is as you've shown above:

function(&array[10]);

And the second is just do the pointer arithmetic directly:

function(array + 10);

In the first case the & is required, since as you mentioned in your question the [] causes the pointer to be dereferenced - the & undoes that operation. What you appear to be confused about are the real semantics of the [] operation. It both does pointer arithmetic and then dereferences the result - you're not getting an address out of that. That's where the & comes in (or just using array + 10 directly).


You are passing by value which means a copy of the variable is sent to the function. In the 2nd case you are passing by reference.

In the second case you are directly modifying the contents at the address of the array plus index.

Check this simple example to know the exact difference.


Your function's signature is probably

void function(table *); // argument's type is pointer to a table 

When you pass record[i], you pass a table object. In order to pass a pointer to a table, you have to pass &record[i], like you did.


Your function is expecting a pointer to the structure. This arguement can be an individual instance of that structure or it could be an element in the array of the give structure. Like

struct myStruct {
     int a, b;
     long cL, dL;
     char e;
} struc1, struc2, record[20];

and function's prototype will be

function( struct myStruct *ptr);

Now you can pass the structure to function:

function( &struct1 );
// or
function( &record[ index] );

Now your confusion arises because of the misconception that syntax array[i] can also be treated as a pointer like we can do with the name of the array.

record - name of the array- gives the address of the first member of the array, (pointers also point to memory addresses) hence it can be be passed to the function. But record[index], it is different.

Actually, when we write record[ index] it gives us the value placed there which is not a pointer. Hence your function which is accepting a pointer, does not accept it.

To make it acceptable to the function, you will have to pass the address of the elements of the array i.e

function( &record[ index ] );

Here & operator gives the address of the elements of the array.

Alternatively, you can also use:

function( record + index  );

Here, as we know record is the address of the first element, and when we add index in it, it gives the address of the respective element using pointer arithmetic.

Hope it was helpful.

0

精彩评论

暂无评论...
验证码 换一张
取 消