Thread: Newbie can't do STRUCTS

  1. #1
    Unregistered
    Guest

    Angry Newbie can't do STRUCTS

    Could someone dig a newbie out of a hole, I am trying to get to grips with structs I have struggled a little bit. I wrote the code below to try and get a working program and give me something to build on. The code below will compile fine but will crash after the user has entered his name. The problem I think is with the typedef the program won't allow me to delare the pointer to the struct globally, but it works fine if declare it locally.
    No doubt the solution is obvious but thank you for your help in advance.

    Terry





    #include<stdio.h>


    typedef struct person{
    char name[21];
    float cost;
    int number;
    }PLAYER;
    PLAYER *me; program will not work if this decleration is used





    int main()
    {
    int a;
    /*PLAYER *me;*/ program works if this decleration is used

    printf("insert your name\n\n");
    scanf("%s",me->name);
    printf("your name is %s\n\n", me->name);




    scanf("%d",&a);/*KEEPS THE WINDOW OPEN*/
    return (0);
    }

  2. #2
    Unregistered
    Guest
    You have declared your struct and created a pointer to it, but you have not allocated any memory for it. (*hint* malloc)

  3. #3
    Unregistered
    Guest
    Thanks for the help, problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  3. Newbie: An Array of Structs from a .txt file?
    By Swerve in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2008, 11:23 PM
  4. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM