Results 1 to 3 of 3

Thread: pointer to a struct

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2001
    Location
    UK
    Posts
    205

    pointer to a struct

    i have this struct:

    struct temp_rec
    {
    int iYear;
    int iMonth;
    int iTemp;
    int iLoc;
    char* sLoc;
    }*trPtr;

    and im trying to write values into the pointer and read from the pointer like this:

    printf("Please enter the location: ");
    scanf(trPtr->sLoc);

    its causing a stack dump so i know there is something wrong. There is nothing that goes near the struct anywhere else in the program. what am i doing wrong? I want to use pointers to conserve memory usage. plus i havent really used pointers much, i could do with the practice.

  2. #2
    jim mcnamara
    Guest
    You have to have the pointer aimed at memory where the datatype lives. Here is one way to do that:
    Code:
    struct temp_rec 
    { 
    int iYear; 
    int iMonth; 
    int iTemp; 
    int iLoc; 
    char* sLoc; 
    }*trPtr, mystruct;
    trPtr = &mystruct;

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    And you have to allocate memory for the string.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width