Hey there! Sign in to join this conversationNew here? Join for free

Help with C code please. Watch

Announcements
    • Thread Starter
    Offline

    3
    ReputationRep:
    struct computer{
    float cost;
    int year;
    int cpu_speed;
    char cpu_type[16];

    };

    typedef struct computer SC;
    void datareceive(SC *ptr_s);

    int main(){

    SCmodel;
    datareceive(&model)
    printf("Here are what you have entered: \n";
    printf("Year: %d\n", model.year);
    printf("Cost: $%6.2f \n", model.cost);
    printf("CPU type: %s \n", model.cpu_type);
    printf("CPU speed %d Mhz\n", model.cpu_speed);

    return(0);

    }

    SC datareceive(SC *ptr_s){

    printf("the type of the CPU inside yourcomputer? \n";
    gets((*ptr_s).cpu_type);
    printf("The speed MHz of the CPU \n";
    scanf("%d", (*ptr_s).cpu_speed);
    printf("The year your computer was made \n";
    scanf("%d", (*ptr_s).year);
    printf("How much did you pay for thecomputer?" \n);
    scanf("%f", (*ptr_s).cost);

    }


    So we are passing in a pointer to the function here.
    But, what i don't understand is how can we pass an address into the argument *ptr_s?
    Isn't *ptr_s the value at the location pointed to by ptr_s, which is no where?
    • Thread Starter
    Offline

    3
    ReputationRep:
    Anyone?
    Offline

    15
    ReputationRep:
    http://www.whaleoil.co.nz/wp-content...er-says-no.jpg
    • Thread Starter
    Offline

    3
    ReputationRep:
    Lol idiot.
    Offline

    17
    ReputationRep:
    *ptr when in a function argument just means it's accepting a pointer as a parameter.
    *ptr when in a statement means dereference the pointer(get the value it's pointed at) the behaviour is contextual.

    You are not receiving the value of the pointer as an argument you are receiving the pointer itself which is why it's valid.

    If you pass a pointer that points nowhere(because it wasn't initialised or the memory got deallocated then dereference bad things happen -like it crashes 30minutes later for no obvious reason it's undefined so anything could happen like Cthulu rising from the dead.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Should Spain allow Catalonia to declare independence?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.