Like Tree1Likes
  • 1 Post By quzah

Why does C assign values to arrays like this?

This is a discussion on Why does C assign values to arrays like this? within the C Programming forums, part of the General Programming Boards category; Hi all, I am a java programmer learning C and was quite shocked when I wrote this simple array assignment ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Why does C assign values to arrays like this?

    Hi all,
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program. C seems to assign values to arrays???

    Program:
    Code:
    #include <stdio.h>
    
    int main(void) {
     
     int values[10];
     int index;
     
     values[0] = 197;
     values[1] = 0;
     values[2] = -100;
     values[5] = 350;
     values[3] = values[0] + values[5];
     values[9] =
     values[5] / 10;
     --values[2];
     
     for (index = 0; index < 10; ++index)
    printf ("values[%i] = %i\n" , index , values[index]);
    
     getch();
     return 0;   
        
        
        
    }
    Output:

    values[0] = 197
    values[1] = 0
    values[2] = -101
    values[3] = 547
    values[4] = 0
    values[5] = 350
    values[6] = 2293616
    values[7] = 212
    values[8] = 204
    values[9] = 35


    I am using DevShed as IDE.

    Thanks for any feedback.

    /Can

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,315
    Quote Originally Posted by FromJavatoC
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program. C seems to assign values to arrays???
    What made you conclude that? Also, what does "assign values to arrays" actually mean to you?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Actually I figured it out. You shouldn't assume any value for uninitialized arrays.
    Assign value = initialize.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,315
    Quote Originally Posted by FromJavatoC
    You shouldn't assume any value for uninitialized arrays.
    Ah, yes. In general, you should not access the value of a variable until the variable has been given an initial value.

    Quote Originally Posted by FromJavatoC
    Assign value = initialize.
    Not quite. Array initialisation syntax would be something like:
    Code:
    int values[10] = {197, 0, -100};
    The remaining elements would be zero initialised.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Yes , I think I get it now.

    thanks.

    /Can

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by FromJavatoC View Post
    Yes , I think I get it now.

    thanks.

    /Can
    What you were seeing in your original test run were the junk values in the memory where the array was created ( in this case on your program's stack).

    C is not Java... it does not babysit... there are no initializers, there is no garbage collector, there is no memory management, there is no type checking or bounds checking. If you want these things you have to do them yourself.

    One thing I can promise you is that C will make you into a very careful programmer... a skill seldom learned in managed code environments.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,679
    Quote Originally Posted by CommonTater View Post
    C is not Java... it does not babysit... there are no initializers,
    C does initialise in some circumstances but not in others. Apart from that, you are correct. It is generally better to assume any variable you have not initialised needs to be initialised before you use it.

    Quote Originally Posted by CommonTater View Post
    One thing I can promise you is that C will make you into a very careful programmer... a skill seldom learned in managed code environments.
    I wish that was true.

    I would suggest the majority of C programmers I have seen are careless. There are also too many programmers who program to one particular compiler and have kittens when their code doesn't work with another compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by grumpy View Post
    C does initialise in some circumstances but not in others. Apart from that, you are correct. It is generally better to assume any variable you have not initialised needs to be initialised before you use it.
    That's kind of like assuming the gun is empty.... I always intialize everything...

    I would suggest the majority of C programmers I have seen are careless. There are also too many programmers who program to one particular compiler and have kittens when their code doesn't work with another compiler.
    If you take a look at the Pelles C forums, you'll recognize one of the authors of addins and libraries... But I don't ever assume my stuff will work anywhere but on Pelles C...

    Portability is not generally an issue for me... If you've got source code for one of my bigger projects, then you're the thief who broke into my house and raided my safe... (i.e. I am no fan of "open source")

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    That's kind of like assuming the gun is empty.... I always intialize everything...
    I assume all guns are loaded, but with unknown ammunition. It may be too big to fit through the barrel, and blow up in my hand. I'd better check it myself.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by FromJavatoC View Post
    I am a java programmer learning C and was quite shocked when I wrote this simple array assignment program
    Java: Oh hai guyz!
    C#: OMG HI DUDE!
    C++: Hi kids.
    C: All of you get off my lawn!


    Quzah.
    CommonTater likes this.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Java: Oh hai guyz!
    C#: OMG HI DUDE!
    C++: Hi kids.
    Pascal: Iligitimi non carborundum!
    C: All of you get off my lawn!

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I was going to reply with something ASM said, but there were two dialects, neither of which I could understand.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I was going to reply with something ASM said, but there were two dialects, neither of which I could understand.
    Quzah.
    Java: Oh hai guyz!

    C#: OMG HI DUDE!

    C++: Hi kids.

    Pascal: Iligitimi non carborundum!

    ASM : Well, first I became aware of a secondary presence within my field of concern, then I incremented my attention pointer to achieve a value just short of bit level overflow, noticing that my accumulator was xor ed with my stack pointer as the experience mounted so I called a long jump to a subroutine designed to initialize my oratory mechanism, next I loaded a string from memory and looped across it causing an audible greeting of some inane kind then finally I settled back initializing my auditory input and preparing to record a timpanic greeting from the being in my newly expanded field of experience.

    C: All of you get off my lawn!
    Last edited by CommonTater; 08-12-2011 at 06:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assign values to a set of array of structures
    By glucosonte in forum C Programming
    Replies: 1
    Last Post: 08-26-2009, 09:10 AM
  2. Assign an arrays values to another array
    By laczfinador in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 08:46 AM
  3. assign values to string arrays
    By paulur in forum C Programming
    Replies: 3
    Last Post: 03-22-2006, 01:37 AM
  4. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 01:02 AM
  5. assign values to charechter variables
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2001, 08:55 PM

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