Thread: Can I insert values in an array using "scanf" through the use of pointers?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    Can I insert values in an array using "scanf" through the use of pointers?

    Hello!
    I was wondering if I could insert values in an array using "scanf" through the use of pointers.
    I made this code

    Code:
    #include<stdio.h>#define SIZE 4
    
    
    int main()
    {
        int arry[SIZE];
        int *arryPtr = arry;
        int i;
    
    
        for( i = 0; i < SIZE; i++ )
        {
            printf( "Enter in Index %d: ", i );
            scanf( "%d", arryPtr );
        }
    
    
        printf( "\n\n" );
    
    
        for( i = 0; i < SIZE; i++ )
        {
            printf( "%d\n", arryPtr[ i ] );
        }
    this gives an output like:
    Code:
    Enter in Index : 1
    Enter in Index : 2
    Enter in Index : 3
    Enter in Index : 4
    
    4
    -858993460
    -858993460
    -858993460
    
    Press any key to continue  .  .  .
    please help me, what should I use?
    pointer or array in "scanf"?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yes. Arrays and index are just syntactic sugar for pointers and offsets (with the offsets themselves being a pointer sometimes).

    Computers run (under the hood), on addresses and clock sweeps. LOTS of addresses and clocks sweeps. That's why any language that works low level with the hardware, needs to have pointers. They are hugely powerful, but much more difficult for people to work with than an array with an index.

    You don't need to use scanf() however. You can assign a value to any place in the array, directly. That's true with pointers, and also with indices.

    You can use scanf(), but you wouldn't normally want to. More work than is needed. Scanf() needs the address of the object it will be changing, so pointer or &array[index], either one.
    Last edited by Adak; 11-16-2011 at 05:53 AM.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    Denmark
    Posts
    80
    You can use pointer or array, both will work. The problem in your example is that you're alway writing on the first block of you're array, since arryPtr is pointing to the address of the first block of the array. You can either directly use your array with "&arry[i]", or use the arryPtr with "(array + i)", so you write in the next block at each iteration of your "for" loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "insert sort with head and tail pointers"
    By noway2 in forum C Programming
    Replies: 2
    Last Post: 10-24-2011, 10:18 PM
  2. scanf( "%s", &array )
    By Else in forum C Programming
    Replies: 19
    Last Post: 09-15-2008, 02:04 PM
  3. deletion of pointers do not "nullify" values.
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 10-03-2007, 10:16 AM
  4. Replies: 2
    Last Post: 05-10-2005, 07:15 AM
  5. Essentially an "array of pointers" problem
    By Matt13 in forum C Programming
    Replies: 4
    Last Post: 03-12-2004, 03:36 AM