Thread: pointers and structures

  1. #1
    bukko
    Guest

    Unhappy pointers and structures

    Hi

    My problem is this: If you declare a structure and then pass a pointer to it, to a function like this -

    struct computer {
    float cost;
    int year;
    char cpu_type[16];
    };
    typedef struct computer SC

    void datain(SC *ptr_d); /*declare function*/

    main()

    SC model; /*struct variable declared*/

    datain(&model) /*here is my problem*/

    you have said in your function declaration you will pass a pointer of the SC type later on. However if you pass &model, thats not a pointer is it? Its just the address of the model struct variable.

    Isnt a pointer supposed to be a memory address which contains a memory address. The content memory address pointing to the left side of your variable?

    I am rather confused here can anyone help?

    Thanks

    Bukko

  2. #2
    Unregistered
    Guest
    you need to declare a pointer to your structure and pass that

    SC model;
    SC *model_ptr;

    model_ptr = &model;

    datain(model_ptr);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > you have said in your function declaration you will pass a pointer of the SC type later on.
    Thats correct.

    > However if you pass &model, thats not a pointer is it?
    Yes it is.

    > Its just the address of the model struct variable.
    Which is a pointer.

    > Isnt a pointer supposed to be a memory address which contains a memory address.
    A pointer is a variable which contains the address of some other variable.

    Code:
    SC model;
    SC *ptr_to_model = &model;
    datain ( &model );  // get the address of model, and pass that address to the function
    datain ( ptr_to_model );  // read the contents of the variable, and pass that to the function (ie the value of &model)
    Both mean and do exactly the same thing.

    The advantage of the ptr_to_model method is that it allows you to dynamically create new SCs as and when you need them (pointing at an existing object doesn't add much).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    bukko
    Guest
    Thanks guys that has made the whole thing much clearer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM