Thread: Help with C Arrays

  1. #1
    Registered User
    Join Date
    Jan 2021
    Posts
    2

    Help with C Arrays

    Im trying to write a program that breaks down a large array into smaller arrays. For the example i'm posting, I made a 4int array to be brokend into 2 2-int arrays.

    Whenever I run my function to fill the two arrays, it always fills the first array just fine, but then rewrites the first array with the second two integers instead of storing them in the second array.

    Why is the fill_array function not using the address of the second array?

    Code:
    #include <stdio.h>
    
    
    void fill_ary(int ary_s[], int ary_d[], int cnt);
    void print_small_ary(int sary[]);
    
    
    
    
    int main(void){
        setbuf(stdout, NULL);
    
    
        int original_ary[4] = {1,2,3,4};
    
    
        int small_ary_a[2] = {0};
    
    
        int small_ary_b[2] = {0};
    
    
        fill_ary(original_ary, small_ary_a, 0);
        fill_ary(original_ary, small_ary_b, 2);
    
    
        print_small_ary(small_ary_a);
        print_small_ary(small_ary_b);
    
    
    }
    
    
    void fill_ary(int ary_s[], int ary_d[], int cnt){
        int i;
        for(i = cnt;i<cnt+2;i++)
            ary_d[i] = ary_s[i];
    }
    
    
    void print_small_ary(int sary[]){
        int i;
        i = 0;
        for(i=0;i<2;i++)
            printf(" %i", sary[i]);
        printf("\n");
        return;
    }
    Output:
    3 4
    0 0

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,311
    The problem is that when you write to ary_d, you must start from index 0, whereas when you read from ary_s, you must start from index cnt. Unfortunately, you start from index cnt in both cases, hence when cnt is greater than 0, you end up writing to the destination array out of bounds.

    I suggest that you have 5 parameters:
    • const int source[]
    • int dest[]
    • int source_start
    • int source_end (or source_count)
    • int dest_size

    This will allow you to write a "copy from source array starting at some index to fill destination array" function while avoiding array out of bounds accesses.
    Last edited by laserlight; 01-31-2021 at 06:59 PM.
    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

  3. #3
    Registered User
    Join Date
    Jan 2021
    Posts
    2
    Quote Originally Posted by laserlight View Post
    The problem is that when you write to ary_d, you must start from index 0, whereas when you read from ary_s, you must start from index cnt. Unfortunately, you start from index cnt in both cases, hence when cnt is greater than 0, you end up writing to the destination array out of bounds.

    I suggest that you have 5 parameters:
    • const int source[]
    • int dest[]
    • int source_start
    • int source_end (or source_count)
    • int dest_size

    This will allow you to write a "copy from source array starting at some index to fill destination array" function while avoiding array out of bounds accesses.
    That worked great - Thank you so much for the explanation!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays and string arrays to functions
    By codeer in forum C Programming
    Replies: 2
    Last Post: 03-25-2015, 10:07 AM
  2. Replies: 9
    Last Post: 07-11-2013, 10:57 PM
  3. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 12:07 AM
  4. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread