简体   繁体   中英

How to print the values that are stored in the array using pointer and function in c

I am new to the pointer in c, I have done the following simple array programme using the pointer.

#include<stdio.h>
void disp(int *);
void show(int a);

int main()
{
    int i;
    int marks[]={55,65,75,56,78,78,90};
    for(i=0;i<7;i++)
        disp(&marks[i]);
    return 0;
}
void disp(int *n)
{
    show((int) &n);
}
void show(int a)
{
    printf("%d",*(&a));
}

I want to get all these values that are stored in the array as output but I only get the memory number of these stored value in the array. plz, help me how to get the array values as output.

I guess you want to play with pointers.

Please note that void show(int a) expects int value. Therefor you do not have to do anything to a to print it. *(&a) is equivalent to a . &a gets the address of a and * dereference the pointer.

Of course it is possible that pointer entering disp(int *n) is passed down the road and dereferenced later. This is ilustrated by calling show1 function inside disp .

#include <stdio.h>
#include <string.h>

void disp(int *);  //  function disp receives the address on int value 

void show(int a);
void show1(int *a); // function show1 will receive the address of n

int main()
{
    int i;
    int marks[]={55,65,75,56,78,78,90};

    for(i=0;i<7;i++)  // 7 since you want to print all elements

        disp( &marks[i] );

    return 0;
}

void disp(int *n)
{
    show(*n); // show expects the 'int' value therefore we have to dereference the pointer. 
    show1(n); // function show1 will receive the address of n and will dereference the pointer inside the function
}

void show(int a)
{
    printf("%d ",a);
}

void show1(int *n) // show1 gives the output of the value that is stored in address n
{
    printf("%d\n",*n);  // dereference the address n to print the value
}

Output:

55 55
65 65
75 75
56 56
78 78
78 78
90 90

& always gives you the memory address of the variable. So &n is giving you the memory address of the variable n .

If you want the value of a pointer, use * . To get the value stored by the pointer n , you want to use (int)*n . Of course you don't need the cast at all, just *n .

I recommend going over some tutorials in C/C++ pointer basics. Pointers are a basic skill you want to have a solid foundation of.

If you just wanted to print all the elements of that array, you just need to do

for(i=0;i<7;i++)
    printf("%d", marks[i]))

Note that there are 7 elements in marks , the exit condition in the loop should be either i<7 or i<=6 and not i<6 .


You are sending the address of a variable to the function disp() as n .

The n in disp() acts similar to a local variable of that function except that it gets its value from the function calling it.

So n is stored somewhere in the memory and hence has an address. This address is what you get when you do &n . So what you see is probably meaningless (because they are allocated on stack memory ).

You do an explicit type cast to int on this address with (int) &n whose value you then pass to show() . Read about explicit type casting here .

In show() , you first take the address of a with &a and then find the value in that address with *(&a) which is the same thing as a (think along the lines of -(-a) ie, taking the negative of a number whose negative is in turn found which is the same number you started off with).

&--> get me the address.

*--> get me the value at that address

disp(&marks[i])--> get the address of variable "marks[i]" and pass it to *n(disp(int *n)), so that when *n is executed it will get the value at the address it has been assigned.

show((int) &n)--> get address of the variable that stores the address of the variable marks[i].

To make it work as intended, it must be show(*n)--> pass the value at the address pointed to by n. (Typecasting is not needed as you're passing an int value to show())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM