I'm trying to create a function that modifies a 2D array...

This is a discussion on I'm trying to create a function that modifies a 2D array... within the C Programming forums, part of the General Programming Boards category; I'm trying to write a function that inputs an array of 65 elements (ArrA[65]) and a two-dimensional array of Nx65 ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    I'm trying to create a function that modifies a 2D array...

    I'm trying to write a function that inputs an array of 65 elements (ArrA[65]) and a two-dimensional array of Nx65 elements (ArrB[N][65]) and returns the two dimensional array with the one dimensional array appended to the end. I'm keeping track of N in the index ArrB[0][0] (with the rest of the first row full of -1s). I know I'm going to have to malloc/calloc something, but I'm not sure how to prevent the new value from being a local variable. Any ideas?

    Would I need to do something like create a temporary array (ArrC) the size of ArrB, then do
    Code:
    ArrB = (int*) calloc( (N+1)*65, sizeof(int));
    Then just iterate through assigning all of the values of ArrC to it and assign ArrA at the end?

    Sorry, I'm not sure how to make sure ArrB stays a 2D array. Any help is greatly appreciated!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    define ArrB as an object of type int**, allocate it dynamically, and then assign to it

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2
    Quote Originally Posted by itCbitC View Post
    define ArrB as an object of type int**, allocate it dynamically, and then assign to it
    I'm sorry, but I've never used objects. How exactly does this work? I didn't think I could allocate dynamically in C.


    Edit: Would it also be possible to just use realloc? I'd only be able to access this as a 1D array, right?

    Code:
    ArrB = (int*) realloc (ArrB, (N)*65 * sizeof(int));
    Here is what I have so far (just for debugging):

    Code:
    #include <stdio.h>
    #include <stdlib.h>#define WIDTH       5
    #define loc(r,c)    (((r-1)*WIDTH)+(c-1))
    }
    
    int main(){
        int CP[5] = {6,7,8,9,10};
        int AP[loc(3,5)+1] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    
        AP = (int*) realloc(AP, sizeof(AP)+sizeof(CP));
    }
    But I am getting the error "error: incompatible types in assignment of `int*' to `int[15]'." What does this mean? I thought I have everything cast correctly.
    Last edited by TheBiles; 02-25-2010 at 06:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Passing 2D array to a function
    By Krupux in forum C Programming
    Replies: 4
    Last Post: 09-04-2003, 07:08 AM
  5. passing an array to a function (2d array)
    By revelation437 in forum C Programming
    Replies: 5
    Last Post: 03-11-2003, 02:32 PM

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