malloc 2D array

This is a discussion on malloc 2D array within the C Programming forums, part of the General Programming Boards category; hi all I am using the following code, which is printing a value of 0.0 instead of 212.0. What am ...

  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
    435
    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
    21,310
    Remember to use free() on what you malloc().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    deleted, was wrong
    Last edited by Epy; 03-02-2010 at 05:44 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    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, 08: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

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