1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Scanning values to structs in C

  1. Apr 14, 2013 #1
    1. The problem statement, all variables and given/known data

    I do not have much experience with C, just a single course a few years ago, and I didn't do so well in it either.

    I'm just trying to get familiar with structs in C. Basically what I want to do is just store the length and width of a rectangle using a function and a struct.

    I can get it to work without using a function with this code:

    Code (Text):

    #include <stdio.h>
    #include <stdlib.h>


    typedef struct{
       
        int length;
       
        int width;
       
    } Piece;

    int main(void){

       
        int p_count=0;

        printf("How many pieces? ");
        scanf("%d",&p_count);
       
        Piece piece[p_count];
       
        for(int i=0; i<p_count; i++){
            printf("\n\nEnter the length for piece %d: ",i+1);
            scanf("%d",&piece[i].length);
            printf("Enter the width for piece %d: ",i+1);
            scanf("%d",&piece[i].width);
        }
       
        for(int i=0; i<p_count; i++){
            printf("\n\n---------------------------\n");
            printf("Length for piece %d is: %d\n",i+1,piece[i].length);
            printf("Width for piece %d is: %d\n",i+1,piece[i].width);
            printf("---------------------------\n");
        }
        return 0;
    }
     
    However, when I try to move just the part where I get the input into a function called "readPieces" I get an error when trying to store the values into length and width.

    Code (Text):


    #include <stdio.h>
    #include <stdlib.h>


    typedef struct{
       
        int length;
       
        int width;
       
    } Piece;

    void readPieces(Piece piece, int p_count);

    int main(void){

       
        int p_count=0;

        printf("How many pieces? ");
        scanf("%d",&p_count);
       
        Piece piece[p_count];
       
        readPieces(piece[p_count],p_count);
       
        for(int i=0; i<p_count; i++){
            printf("\n\n---------------------------\n");
            printf("Length for piece %d is: %d\n",i+1,piece[i].length);
            printf("Width for piece %d is: %d\n",i+1,piece[i].width);
            printf("---------------------------\n");
        }
        return 0;
    }

    void readPieces(piece[p_count], int p_count){
        for(int i=0; i<p_count; i++){
            printf("\n\nEnter the length for piece %d: ",i+1);
            scanf("%d",&piece[i].len);
            printf("Enter the width for piece %d: ",i+1);
            scanf("%d",piece[i].wid);
        }
    }

     
    2. Relevant equations



    3. The attempt at a solution

    I'm sure that my mistake is something about how I call the function, or how I'm trying to store the values. But I do not have enough experience to debug this myself.

    If anyone can help out with understand the proper way to call a function using a struct and store user input it would be greatly appreciated.
     
  2. jcsd
  3. Apr 14, 2013 #2

    Borek

    User Avatar

    Staff: Mentor

    What kind of error?

    Code (Text):
    scanf("%d",&piece[i].length);
    and

    Code (Text):
    scanf("%d",&piece[i].len);
    are not exactly identical.
     
  4. Apr 14, 2013 #3

    rcgldr

    User Avatar
    Homework Helper

    piece[p_count] is an attempt to access beyond the end of the array.

    Try this:

    Code (Text):

        readPieces(piece, p_count);

    ...

    void readPieces(PIECE *piece, int p_count){
     
     
  5. Apr 14, 2013 #4
    Sorry I forgot to change that. Originally I had it as &piece.len but changed to .length just for the post.

    My error comes from Xcode, saying "The subscripted value is not a pointer, array, or vector" and it underlines &piece.length and also &piece.width
     
  6. Apr 14, 2013 #5
    Thank you SO much! This worked perfectly. If you wouldn't mind, could you explain what you mean by access beyond the end of the array? If p_count is just defined by the input, I can't seem to understand how could it be outside of the array.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Scanning values to structs in C
Loading...