problem passing pointer array

This is a discussion on problem passing pointer array within the C Programming forums, part of the General Programming Boards category; Hi, can some one please tell me why this program is not able to function properly. I have a array ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    87

    problem passing pointer array

    Hi, can some one please tell me why this program is not able to function properly. I have a array a and i am trying to create a pointer array b which points to elements less than 40 in a. I got a warning from the compiler:

    warning #2116: Local 'b' is used but never assigned a value.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    void create_ptr_list(int *a, int **b, int n, int *size_ptr)
    {
        int i;
        *size_ptr = 0;
    
         for (i = 0; i < n; i++)
         {
             if (a[i] < 40)
               *size_ptr ++;
         }
    
         *b = malloc(sizeof(int *) * (*size_ptr));
         *size_ptr = 0;
          for (i = 0; i < n; i++)
         {
             if (a[i] < 40)
               b[*size_ptr ++] = &a[i];
          }
    
     }
       
    int main(void)
    { 
       int a[] = { 5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
       int **b;
       int size;
       int i;
    
        create_ptr_list(a, b, 10, &size);
        for(i = 0; i <size; i++)
          printf("&#37;d\n", *b[i]);
    
        return (0);
    }
    Last edited by broli86; 06-30-2008 at 10:14 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    Instead of defining b in main() as a int**, define it as an int*, then pass its address:
    Code:
    int *b;
    /* ... */
    create_ptr_list(a, &b, 10, &size);
    for(i = 0; i < size; i++)
        printf("&#37;d\n", b[i]);
    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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by broli86 View Post
    warning #2116: Local 'b' is used but never assigned a value.
    In other words:
    You are using the variable "b", but you never initialized it, so it is guaranteed to contain junk that's passed on that whatever you're using it on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by laserlight View Post
    Instead of defining b in main() as a int**, define it as an int*, then pass its address:
    Code:
    int *b;
    /* ... */
    create_ptr_list(a, &b, 10, &size);
    for(i = 0; i < size; i++)
        printf("%d\n", b[i]);
    But declaring it as *b wouldn't create an array of pointers, would it ? I needed an array of pointers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by Elysia View Post
    In other words:
    You are using the variable "b", but you never initialized it, so it is guaranteed to contain junk that's passed on that whatever you're using it on.
    but b is being initialized in the create_ptr_list function ? I allocated memory and initialize b.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    But declaring it as *b wouldn't create an array of pointers, would it ? I needed an array of pointers.
    In that case you change it back, but you still need to pass the address of b to create_ptr_list(), so create_ptr_list() must be changed.

    but b is being initialized in the create_ptr_list function ? I allocated memory and initialize b.
    You allocated memory for and initialised the b in create_ptr_list(), but since you did not pass the address of the b in main() to create_ptr_list(), create_ptr_list() changes what the b in main() points to. Since the b in main does not point to a valid object, you have undefined behaviour.
    Last edited by laserlight; 06-30-2008 at 09:58 AM.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Then define it as
    int** b
    and pass its address &b
    and define it as int*** b in create_ptr_list.

    If you don't pass the address to the variable, how do you expect main to retain its value after create_ptr_list ends?

    Quote Originally Posted by broli86 View Post
    but b is being initialized in the create_ptr_list function ? I allocated memory and initialize b.
    No, you initialized the local variable b in create_ptr_list, which is not tied to main because you did NOT pass its address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    still not working..

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    void create_ptr_list(int *a, int ***b, int n, int *size_ptr)
    {
        int i;
        *size_ptr = 0;
    
         for (i = 0; i < n; i++)
         {
             if (a[i] < 40)
               (*size_ptr) ++;
          }
    
         *b = malloc(sizeof(int *) * (*size_ptr));
          *size_ptr = 0;
          for (i = 0; i < n; i++)
         {
             if (a[i] < 40)
             *b[*size_ptr++]= &a[i];
          }
    
     }
       
    int main(void)
    { 
       int a[] = { 5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
       int **b;
       int size;
       int i;
    
        create_ptr_list(a, &b, 10, &size);
        for(i = 0; i <size; i++)
          printf("%d\n", *b[i]);
    
        return (0);
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    You changed the function signature, but did not change its implementation likewise.
    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

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    87
    Quote Originally Posted by laserlight View Post
    You changed the function signature, but did not change its implementation likewise.
    ok, where am i going wrong ?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,271
    ok, where am i going wrong ?
    This line is problematic:
    Code:
    *b[*size_ptr++]= &a[i];
    It should be:
    Code:
    (*b)[(*size_ptr)++] = &a[i];
    But I would rather create say, a pointer and then use that:
    Code:
    int **b_ptr;
    
    /* ... */
    
    b_ptr = *b;
    for (i = 0; i < n; ++i)
    {
        if (a[i] < 40)
        {
            *b_ptr++ = &a[i];
        }
    }
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  3. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  5. passing 2d array as pointer
    By X PaYnE X in forum C Programming
    Replies: 2
    Last Post: 12-03-2005, 09:28 AM

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