Pointer points to struct pointer

This is a discussion on Pointer points to struct pointer within the C Programming forums, part of the General Programming Boards category; Hi, I made a program which can be compiled but not built and run. The VC++ compiler said that it ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Red face Pointer points to struct pointer

    Hi,

    I made a program which can be compiled but not built and run.

    The VC++ compiler said that it has unresolved external error.

    Here is my code:

    #include <stdio.h>

    //pointer has pointers

    void main()
    {
    struct int_pointers
    {
    int *ptr1, *ptr2;
    };

    struct int_pointers *point, ptrs; //create 2 objects.

    point = &ptrs;

    int i1 = 123, i2 = 87, i3 = 45;

    *(*point).ptr1 = 12; //dereference point and use dot notation

    point->ptr2 = &i2; //pointer points pointer

    printf("i1 = %d, point->ptr1 = %d\n", i1, *(*point).ptr1); //12

    printf("i2 = %d, point->ptr2 = %d\n", i2, *point->ptr2); //87

    }

    Anyone knows why?

    Thanks

    gogo

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    I am not sure what the 'unresolved external error' is from. The compiler should list a specific name for the error. Could be that you are missing a library file. Maybe try changing your main() to return an int value.
    Code:
    int main()
    {
        /* ..... code ....*/
        return 0;
    }
    Also, your program may compile but it is not going to run anyway. You are attempting to assign a constant value to a dereferenced pointer that has not been assigned any memory. You will have to change you code to look something like this:
    Code:
    #include <stdio.h> 
    #include <malloc.h>
    
    //pointer has pointers 
    
    int main() 
    { 
        struct int_pointers 
        { 
            int *ptr1, *ptr2; 
        }; 
    
        struct int_pointers *point, ptrs; //create 2 objects. 
    
        point = &ptrs;
    
        int i1 = 123, i2 = 87, i3 = 45; 
    
        (*point).ptr1 = (int *)malloc(sizeof(int));
        *(*point).ptr1 = 12; //dereference point and use dot notation 
    
        point->ptr2 = &i2; //pointer points pointer 
    
        printf("i1 = %d, point->ptr1 = %d\n", i1, *(*point).ptr1); //12 
    
        printf("i2 = %d, point->ptr2 = %d\n", i2, *point->ptr2); //87
    
        free ((*point).ptr1);
    
        return 0;
    }
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    I don't think it is related to the malloc issue as (1) I can run this code one mth ago, (2) It shows the same errors even I added (*point).ptr1 = (int *)malloc(sizeof(int));.

    To be more specific, the errors are:

    Linking...
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    Debug/ex.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    ex.exe - 2 error(s), 0 warning(s)

    I used VC++ compiler and run on win2k prof.

    Any idea?

    Thanks a lot for your help.

    gogo

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    LIBCD.lib(wincrt0.obj) : error LNK2001: unresolved external symbol _WinMain@16
    This means that the compiler is looking for the WinMain() function which is the driver for Windows-based applications, i.e. you are trying to build a Win32 Application (Windows). You need to change to a Win32 Console Application.
    The best thing to try is to create a new Win32 Console Application and add your existing .cpp file to the new project. This should clear it up.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Thanks and it works now.

    gogo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to declare a pointer that points to another pointer?
    By yuzhangoscar in forum C Programming
    Replies: 1
    Last Post: 09-15-2008, 06:28 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. pointer to a struct
    By ramdal in forum C Programming
    Replies: 13
    Last Post: 12-15-2005, 08:01 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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