0

i have tried a c program on array and pointers

#include<stdio.h>

int main()
{
    int a[10], i;
    for(i=0; i<10; i++)
    {
        a[i]=0;
    }
    fun(a,i);
}

fun(int *p, int i)
{
    for (i=0; i<10; i++)
    {
        printf("%d\n", &*(p+i));
    }
}

output is:

2752228
2752232
2752236
2752240
2752244
2752248
2752252
2752256
2752260
2752264

it prints addresses instead of array elements

vgru
  • 49,838
  • 16
  • 120
  • 201
  • 3
    Remove the `&`. Dunno why you are passing `i` to `fun` though... – Eugene Sh. Feb 28 '18 at 15:34
  • You print addresses instead of values. *p is the value, &(*p) is the address – Fabio_MO Feb 28 '18 at 15:37
  • Tip: [Suggested reading](https://stackoverflow.com/q/381542/589924) – ikegami Feb 28 '18 at 15:38
  • BTW, `fun` has no prototype. It should throw some warnings – Eugene Sh. Feb 28 '18 at 15:39
  • What Eugene Sh. means is that `fun` is called before it's declared. That's a problem. Either change the order of your functions, or add `void fun(int *p, int i);` near the top to declare `fun` before it's defined. – ikegami Feb 28 '18 at 15:40
  • For start, turn on all warnings and make the compiler report them as errors. [This is what the compiler would tell you then](https://godbolt.org/g/ZGYDt2): 1) implicit declaration of function `fun` (**no prototype for `fun`**), 2) `fun` return type defaults to `int` (**you forgot `void` at the beginning**), 3) format `%d` expects argument of type `int`, but argument 2 has type `int *` (**you are passing a pointer instead of a number**), 4) control reaches end of non-void function (**compiler thinks `fun` should return `int`**). – vgru Feb 28 '18 at 15:41
  • 1
    `&` and `*` are opposite operators. `&*p == p` and `*&p == p` as well – Jean-François Fabre Feb 28 '18 at 15:49
  • print isn typesafe. If You are not sure, what it is type, use itermedaite variable. Compiler give kind of warning – Jacek Cz Feb 28 '18 at 16:47

2 Answers2

5

* and & together here nullify each other's effect. &*(p+i) is equivalent to (p+i). (p+i) gives the address of the ith element of the array. To access the element's value you need to remove & from that expression, which will be *(p+i) and is equivalent to p[i].


Note: Instead of using a loop to initialize all elements of an array you can use initializer list as follows

int a[10] = {0}; 

Also you have to pass size of the array to the function. Your function should use that size to know the length of the array passed.

fun(int *p, int size){
    for (int i=0; i < size; i++){
        printf("%d\n", p[i]);
    }
}

Do not forget to add a function prototype before main

void fun(int *p, int size);

and call it

fun(a, sizeof(a)/sizeof(a[0]));
haccks
  • 104,019
  • 25
  • 176
  • 264
2

&*x side by side applied results in x and here that x is p+i. p is the pointer to the first element of the array and added i to it, the value is incremented by sizeof *p here sizeof int which is 4 bytes in your case. More clearly you printed &p[i] in each iteration and that too with wrong format specifier. Use %p format specifier for printing address.

If you want to print the array - remove the address of operator & and instead use the value that you want to print *(p+i) or p[i].

You have passed i though not clear why. You have used hard-coded value for array length. Know that if your intention was to pass the size of the array you could pass sizeof arr/sizeof arr[0] - this term is nothing but the size of the array.

user2736738
  • 30,591
  • 5
  • 42
  • 56