Thread: Would you explain something to me about arrays in C language?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    12

    Would you explain something to me about arrays in C language?

    I have two example codes:

    array1.c example
    Code:
    int main ()
    {
    
        int test_scores[10];
        int i;
    
        printf("please enter the ten test scores now.\n");
    
        for (i=0; i<10; i++)
           {
             printf("#%2d >   ", i + 1);
             scanf ("%d", &test_scores[i]);
            }
         printf("Thank you.\n");
         return 0;
    }
    On this example I understand that there is a variable test_scores that is defined
    as an array that has ten elements. The indices will be integers 0 though 9.


    This program has errors in its boundaries.
    bounds.c

    Code:
    int main()
    {
         int array[10];
         int nextvar = 5;
         int i;
         
         printf( "nextvar is %d.\n", nextvar); /*is output so I should put &, correct?*/
         for ( i=0; i<=10; i++);  /*I do not understand the error here. Is it the <= sign?*
                  array[i]=i;
    
         printf( "nextvar is now %d.\n", nextvar );   /*how should it look?*/
    
         return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't ever put & in output (unless you want to see the address, not the contents).

    Since you only have 10 elements, attempting to access array[10] is an error, yes.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Although not required, I usually write < int main(void) > when I do not take command line arguments. You should read How to define main()-FAQ and come up with how you want to do it.

    Code:
    int main()
    {
         int array[10];
         int nextvar = 5;
         int i;
         
         printf( "nextvar is %d.\n", nextvar); /*is output so I should put &, correct?*/
    You are telling printf you are printing an integer and that integer is what is stored in nextvar. If you put the & in front then you would be printing the memory address of nextvar vice its value.

    Code:
    for ( i=0; i<=10; i++);  /*I do not understand the error here. Is it the <= sign?*
    You answered this yourself here:
    On this example I understand that there is a variable test_scores that is defined
    as an array that has ten elements. The indices will be integers 0 though 9.
    What is the value of array[10]?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    12
    While reading the code is much simpler than actually writing it, sometimes even reading is not so simple! Thank you!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kelleannmccan
    While reading the code is much simpler than actually writing it, sometimes even reading is not so simple!
    Actually, it is typically easier to write code than to read it. When you write code, you would usually have some understanding of what the code does and you get to express it in the way you choose; when you read code, you would usually lack understanding of what the code does and you don't get to choose how it was expressed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The code you write is always written by you (obviously).
    The code you read is mostly written by someone else (especially when you're working in a team).

    Also, code written by you some time ago is fairly indistinguishable from code written by someone else
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    $0.02 ... Output is always easier than Input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this c language code....only 2 parts of it?
    By ankit8946 in forum C Programming
    Replies: 22
    Last Post: 10-31-2009, 03:33 PM
  2. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  3. C Language And A Scripting Language
    By DarkSpy in forum C Programming
    Replies: 9
    Last Post: 06-26-2003, 08:05 AM
  4. Explain!
    By Kelvin in forum Windows Programming
    Replies: 4
    Last Post: 02-07-2002, 09:14 AM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM