Thread: printing array contents

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    5

    Question printing array contents

    i'm trying to print the elements of my array but am getting 4210784 as the result can someone tell me where i'm going wrong with this and what is actual happening.

    Code:
    #include <stdio.h>
    
    int ray[100];
    
    main()
    { 
    
      printf("%i\n",(ray));
      getchar();
    };

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    element that is located at index ind can be retreived as
    ray[ind]

    ray itself is just a pointer, so printing it value you get some random address used to store the array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    so to print the elements of the array would i have to say print from 0 to 99
    Last edited by c_programmer; 12-15-2006 at 11:40 AM.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    yes you need a loop
    Code:
       int ray[100];
       int i;
       for ( i = 0; i < 100; ++i )
           printf("%d\n", ray[i]);
    Kurt

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by c_programmer
    so to print the elements of the array would i have to say print from 0 to 99
    you have to use loop (for will be good here)
    Inside the loop you will print one element on each iteration
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    right tanks

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Make sure that u perform a boundary check (like what ZUK has done ). Because C dosn;t do boundary it. If it goes beyond the size of an array "Segmentation fault" would be the result

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 03:04 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Printing an integer stored in an array
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-18-2002, 02:11 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM