Thread: malloc 2D array

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    24

    malloc 2D array

    hi all

    I am using the following code, which is printing a value of 0.0 instead of 212.0. What am I doing wrong?

    Code:
    double **u;
    int N = 52;
    int i;
    
    
    u = malloc (N * sizeof(double *));;
    for (i = 0; i < N; i++)
        u[i] = malloc(N * sizeof(double));
    
    u[10][10] = 212.0; 
    
    printf("U[10][10] = %4.3f", u[10][10]);

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    It looks right to me. I think your problem must be somewhere else.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You need to provide us with additional details? Is all that code part of main() or is it part of different functions? Is **u a global variable?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    26
    I have copied your code and I executed like the followings.It worked correctly for me.

    Code:
    #include<stdio.h>
    #include<malloc.h>
    main()
    {
    double **u;
    int N = 52;
    int i;
    
    
    u = malloc (N * sizeof(double *));;
    for (i = 0; i < N; i++)
    u[i] = malloc(N * sizeof(double));
    
    u[10][10] = 212.0;
    
    printf("U[10][10] = %4.3f", u[10][10]);
    }
    Output

    U[10][10] = 212.000

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    I too tried your code. Thats work for me.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remember to use free() on what you malloc().
    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
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    deleted, was wrong
    Last edited by Epy; 03-02-2010 at 06:44 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Epy
    %f is for float, %lf is for double, you're using a double.
    DerekC used printf though, not scanf, so %f for double is correct.
    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

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by laserlight View Post
    DerekC used printf though, not scanf, so %f for double is correct.
    Damnit. Thanks, maybe I need to lay off the forums until I wake up.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    24
    I actually tried exactly what you in that main function, all by itself,and didnt get a correct answer. Let me double check one last time, and I will be back in a sec.

  11. #11
    Registered User
    Join Date
    Nov 2009
    Posts
    24
    Ah. I tracked it down. I had to include stdio.h and stdlib.h to supress the errors about casting the malloc as a (double**) and get the correct return. Thanks for the help..I didnt think I needed that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM