array copy problem

This is a discussion on array copy problem within the C Programming forums, part of the General Programming Boards category; I have a basic code where I can copy an array without problems. But when I introduce another kind of ...

  1. #1
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Post array copy problem - [SOLVED]

    I have a basic code where I can copy an array without problems. But when I introduce another kind of array, a more advanced variant which I think is called a dynamic array. Then my program crashes every time.
    What am I doing wrong? What have I misunderstood?


    Array tutorial: C Code Listing 7 - Array tutorial = [ Works ]
    Code:
    #include <stdio.h>
    
    int main()
    {
        short age[4];
        short same_age[4]; 
        int i, j;
    
        age[0]=23;
        age[1]=34;
        age[2]=65;
        age[3]=74;
    
        for(i=0; i<4; i++)
            same_age[i]=age[i];
    
        for(j=0; j<4; j++)
            printf("%d\n", same_age[j]);
    
        system("PAUSE");	
        return 0;
    }

    Improved code 1: = [ It still Works ]
    xnN0TJ17 - Pastebin.com


    Improved code 2: = [ It still Works ]
    GCCALmpP - Pastebin.com


    Improved code 3: = [ FAILED ]
    When I uncomment the /* Copy array */ code then the program doesn't work anymore.
    Why is it happening at this location? What am I doing wrong? What have I misunderstood?
    DsQpH8AX - Pastebin.com
    Last edited by Witch; 03-22-2010 at 06:56 PM. Reason: solved

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is no memory allocated for array_copy.

  3. #3
    Registered User Witch's Avatar
    Join Date
    Mar 2010
    Location
    Stockholm, Sweden
    Posts
    15

    Thumbs up

    Thanks that was quick! You solved 90% of my entire to be program, by that single line.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are welcome. For future reference everytime you are using arrays or structures containing arrays and the program crashes the first thing you should look for is whether you allocated memory for everything you have.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating an array with a piece of a string problem
    By steals10304 in forum C Programming
    Replies: 11
    Last Post: 08-18-2010, 10:26 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 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. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

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