2

Beginner question. I know a bit about pointers but I am having a lot of trouble trying to print values of an array using pointers in a function. Particularly at this line: printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr); My output is a table that displays input count, value entered by user, and the address it is stored. The address looks fine but the values print garbage. I thought the syntax *(arr+i) is correct when trying to increment array of pointers with index i??

#include<stdio.h>
#include <stdlib.h>


void function(int *arr[]);
main()
{
    int array[5];
    function(&array);
}


void function(int *arr[])
{
    int i, n;
    printf("How many elements? ");
    scanf("%d", &n);

    for(i=0; i<n; i++)
    {
        printf("Enter element %d: ", i);
        scanf("%d", arr);
        arr++;
    }

    printf("\nInput\t\tValue\tAddress");
    for(i=0; i<n; i++)
    {
    printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);
    arr++;
    }

}
1
  • int *arr[] is equivalent to int **arr, a pointer to a (array of) pointer. The compiler should have generated a warning at the call, since &array is of type int *. Commented Dec 9, 2014 at 5:46

4 Answers 4

1

int *arr[] is an array of pointers.

You need to allocate memory to these pointers before writing something to them.Like

arr[0] = malloc(sizeof(int));

So your code should look like:

    a[0] = malloc(sizeof(int) *n);
    for(i=0;i<n;i++)
    {
       scanf("%d",(a+i));
       printf("%d\n",*(a+i));
    }

In your code it seems you are just reading value to your array and printing them. You can just use

int *arr = malloc(sizeof(int) *n);

Then do

for(i=0; i<n; i++)
{
    printf("Enter element %d: ", i);
    scanf("%d", &arr[i]);
}
Sign up to request clarification or add additional context in comments.

3 Comments

could you explain the use of malloc for me? i enter that line before for loop and it gives me the error "expected int ** but argument is of type int (*)[5]"
@steeele Do you really array of pointers here? Can't you just use a single pointer as shown?
yeah its part of the exercise for my class. Not only that but i'm really curious what i'm doing wrong and its now puzzling me.
0

Try the following

#include<stdio.h>

#define N 5

void function( int arr[] );

int main( void )
{
    int array[N];

    function( array );
}


void function( int arr[] )
{
    int i, n;
    int *p = arr;

    printf( "How many elements? " );
    scanf( "%d", &n );

    if ( N < n ) n = N;

    for ( i = 0; i < n; i++ )
    {
        printf( "Enter element %d: ", i );
        scanf( "%d", p++ );
    }

    printf( "\nInput\t\tValue\tAddress" );
    for ( i = 0; i < n; i++ )
    {
        printf( "\n%d\t\t%d\t%p", i+1, *( arr + i ) , arr + i );
    }

}

Comments

0

Hope this helps.

#include<stdio.h>
#include <stdlib.h>

  #define N 5
  void function(int *);
  int main()
  {
     int array[N];
     function(&array);
     return 0;
  }


  void function(int *arr)
  {
      int i, n;
       printf("How many elements? ");
       scanf("%d", &n);
       fflush(stdin);

       if (n<=N) {
       for(i=0; i<n; i++)
       {
         printf("Enter element %d: ", i);
         scanf("%d", arr);
         fflush(stdin);
         arr++;
       }

    printf("\nInput\t\tValue\tAddress");
    for(i=0; i<n; i++)
    {
      printf("\n%d\t\t%d\t0x%X", i+1, *(arr+i) , arr);
      arr++;
    }
  }

Comments

0

There are few problems:
1. void function(int *arr[]); => In this function you are declaring an array of pointers, not pointer to an array. Please read about int (*)arr[] and int *arr[], what makes them different is precedence order of "( )" and "[ ]".

2. If you define an array like int arr[5]; then to pass the base address of array you have to just pass it like function(arr) so if you call function like function(&arr) then it does not make any sense. When you declare an array int arr[5] (say) then arr represents the base address of that array always.

==>> Next to work with array of pointer (the one you are using) you need to know about dynamic memory allocation functions like malloc(), calloc(), realloc() and free().

So my advice to you is first read about the pointers from a book that is completely a pointer book. My favourite is "Pointers on C" by Kenneth A. Reek

Don't rely on websites to learn basics, buy a good book. Use StackOverflow as a resource to sove serious problems.

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.