printing array contents

This is a discussion on printing array contents within the C Programming forums, part of the General Programming Boards category; i'm trying to print the elements of my array but am getting 4210784 as the result can someone tell me ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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 10:40 AM.

  4. #4
    ZuK
    ZuK is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,682
    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21