0

I know that the title is not clear at all , but i use this code to tell what i want to ask assume the following simple code :

void example(int *a)
{
    for(i = 0 ; i < 20 ; i++)
    {
       printf(" %d number is %d \n " , i , a[i]);
    }
}
int main()
{
    int a[20] ; 
    // assume that the array is filled 
    example(a);
    return 0 ;
}

so my question is ,what is the 'addresing mode' that c language follow , if we write a[i] ? if we just follow the rules of pointer or syntax of pointer we must write this in this way *a[i] , since we need to show the value that *a point on it , and not the address ?

4 Answers 4

2

The simple rule is that arrays are not pointers. Array names are converted to pointer (in most cases) to its first element when they passed as arguments to a function.

Sign up to request clarification or add additional context in comments.

19 Comments

'in most cases': you've made me curious: at the moment I can't think of an example when it's not so. Could you please help me out?
@IngoLeonhardt; sizeof.
Yepp, but to be pedantic, sizeof ist not a function at all
He meant sizeof(array) will give you the full memory space occupied by the array, not the size of a pointer. sizeof(some ordinary pointer) will always give you 4 bytes for 32-bit architecture and 8 bytes for 64-bit architecture regardless of anything.
@Havenard; No this is the part of the standard. In C, there is no pass by reference.
|
0

About the array subscript operator [], the C99 standard §6.5.2.1¶2 says -

A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).

So a[i] in the function example evaluates to *(a + i). You could write it either way. Please note that arrays and pointers are different types. Arrays are not first-class objects in C unlike integers, floats, structures etc. This means you can't pass array to or return an array from a function.

In the function main, a is an array type - its type is int[20], i.e., an array of 20 integers. When you pass a to the function example in the statement

example(a);

in main, then the array a evaluates to a pointer to its first element which is assigned to function parameter a which is of type int *. You can access the elements in the array a in main by either syntax - a[i] or *(a + i) - they are equivalent as described in the standard. However, remember that the there is a difference in the way the array elements are accessed when using the array itself (a[i] in main) and using a pointer to the first element in the array (a[i] in example). For more details, please look here - Difference between dereferencing pointer and accessing array elements

Comments

0

No when using the []'s it automatically does this. Also it wouldn't make sense to dereference say array[1] and so on since the pointer points to the first index (0).

Comments

0

When you use brackets you are implicitly handling a specific element of the array, so you don't need to head it with * to set its value.

In int *a, a is a pointer. It means it holds a memory address of an int. This can be a lone int or the first element of an array of int. There is no way to know just by having the pointer.

There are several synthaxes you can use to work with this pointer.

By reading or writing to *a, you are handling the int variable that pointer is pointing to. It can also be done by reading or writing to a[0], both means exactly the same thing.

Assuming a points to a sequence of int variables, working with a[1] makes you handle the int stored right next to a[0]. The pointer type is internally used do determine how many bytes next the suceeding int should be in the memory. It will be sizeof(*a) bytes next, or sizeof(int).

Instead of a[1] you could also use *(a + 1). It means the same thing. Such arithmetic operations with pointers are possible, but notice that the compiler will automatically multiply 1 by sizeof(int) internally, so you don't need to be redundant in informing the variable type stored there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.