Thread: Passing uninitialized 2D arrays as arguments in C

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    6

    Passing uninitialized 2D arrays as arguments in C

    Hi, all. I've tried and tried to get my head around this, and feel I am almost there, but I'm getting so confused with how many '*' I need! I have a function that takes as input the string of a directory containing a data file, an int and pointers to two uninitialized 2D arrays. The function reads the data file and then allocates memory and fills the arrays accordingly.

    This code is probably quite wrong, but the idea is:
    Code:
    void main()
    {
      double **Array1;
      int **Array2;
      int dimension1;
      char DirWork[100], buff[100];
    
      f_ReadData(DirWork, dimension1, &Array1, &Array2);
    
      sprintf(buff,"%lf",Array1[0][0]); // Causes segmentation fault
      printf(buff);
    }
    and
    Code:
    void f_ReadData(char *DirWork, int dimension1, double ***Array1ptr, int ***Array2ptr)
    {
      int ct, ct2;
      double **Array1 = *Array1ptr;
      int **Array2 = *Array2ptr;
      char FullDirArray1[100], FullDirArray2[100];
      FILE *d_Array1, *d_Array2;
    
      sprintf(FullDirArray1,"%s%s",DirWork,"Array1.dat");
      sprintf(FullDirArray2,"%s%s",DirWork,"Array2.dat");
    
      d_Array1=fopen(FullDirArray1,"r");
      d_Array2=fopen(FullDirArray2,"r");
      fscanf(d_Array1,"%d", &dimension1);
    
      Array1 = dmatrix(0,dimension1-1,0,3); // allocates memory to Array1 (dimension1 x 3) elements, using nrutil
      Array2 = imatrix(0,dimension1-1,0,3); // allocates memory to Array2 (dimension1 x 3) elements, using nrutil
    
      for(ct=0; ct<dimension1; ct++) 
      {
        for(ct2=0; ct2<3; ct2++)
        {
          fscanf(d_Array1, "%lf", &Array1[ct][ct2];
          fscanf(d_Array2, "%d", &Array2[ct][ct2];
        }
      }
      fclose(d_Array1);
      fclose(d_Array2);  
      Array1ptr = &Array1;
      Array2ptr = &Array2;
    
    }
    I've missed out error handling here, but I do have some of that in place... not that it's helping. I'm getting a segmentation fault when I try to access the arrays from the main function.

    If anyone could help, I'd really appreciate it... I'm seeing *stars! Thank you!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    The pointer code actually looks about right, but the printf is wrong (or at least non-standard). I could be missing something (haven't compiled yet), but most likely, the problem is that you're passing a straight string with no specifiers to printf (wrong) and then using %lf to format it earlier. Replace it with something like this:

    Code:
    printf("%f", Array1[0][0]);
    Note that %f can print either doubles or floats.

    Oh, and replace void main with int main.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by memcpy View Post
    The pointer code actually looks about right, but the printf is wrong (or at least non-standard). I could be missing something (haven't compiled yet), but most likely, the problem is that you're passing a straight string with no specifiers to printf (wrong) and then using %lf to format it earlier. Replace it with something like this:

    Code:
    printf("%f", Array1[0][0]);
    Note that %f can print either doubles or floats.

    Oh, and replace void main with int main.
    Thanks for the suggestion. I somehow didn't know you could print numbers directly without converting them to strings first, so this will come in very handy! However, I don't think that's the only problem, as even when I comment out my sprintf and printf, I get the segmentation fault when I then try to use a value from Array2 (which is filled with ints) as a dimension for a new array...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Check: are you allocating memory for the dynamic arrays?
    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

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Check: are you allocating memory for the dynamic arrays?
    Yes - that's done by nrutil. I know the arrays are set up correctly within f_ReadData, as I can print them out within that function (if I comment out the parts that cause the segmentation fault in main)... I just don't have them in the main function, as I have errors when I try to access them. My best guess is that I must be trying to look at incorrect address space or the address hasn't even been set.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, I see it now:
    Code:
    Array1ptr = &Array1;
    Array2ptr = &Array2;
    You should have written:
    Code:
    *Array1ptr = Array1;
    *Array2ptr = Array2;
    Otherwise you're just getting a pointer parameter to point to a local variable.
    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

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    This post is also on StackOverflow.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by FloridaJo View Post
    This post is also on StackOverflow.
    Yes, it is... but I hadn't got an answer there, and I really needed to get one ASAP. I thought I'd give it a go here, too... and it worked.

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Quote Originally Posted by laserlight View Post
    Oh, I see it now:
    Code:
    Array1ptr = &Array1;
    Array2ptr = &Array2;
    You should have written:
    Code:
    *Array1ptr = Array1;
    *Array2ptr = Array2;
    Otherwise you're just getting a pointer parameter to point to a local variable.
    Thank you so much! I'm still really struggling to fully understand pointers, and obviously got this completely back-to-front. My code is now running perfectly. You're a star!

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You're welcome.

    Next time, when you start a thread for a problem for which you have sought help elsewhere, please provide a link to that other place and inform that other community that you are also seeking help here.
    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

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    6
    Will do. Thank you. I didn't know the etiquette, and didn't mean to seem deceptive.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing uninitialized 2d array to function
    By sangamesh in forum C Programming
    Replies: 3
    Last Post: 06-25-2010, 04:02 AM
  2. Passing arguments by value
    By nedim in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2009, 01:24 AM
  3. Passing arguments.
    By omnificient in forum C Programming
    Replies: 3
    Last Post: 05-28-2008, 02:41 PM
  4. passing arguments using "Command Line Arguments"
    By Madshan in forum C++ Programming
    Replies: 1
    Last Post: 04-19-2006, 03:46 PM
  5. passing arguments
    By kas2002 in forum C++ Programming
    Replies: 12
    Last Post: 07-16-2002, 10:38 AM

Tags for this Thread