Scanning values to structs in C

  • Thread starter mathmannn
  • Start date
  • Tags
    Scanning
In summary, the conversation is about a person trying to get familiar with structs in C and wanting to store the length and width of a rectangle using a function and a struct. They provide two code examples, one that works without using a function and one that causes an error when using a function. The error is due to trying to access beyond the end of the array, which is fixed by passing the array as a pointer in the function.
  • #1
mathmannn
15
0

Homework Statement



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:
#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:
#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);
    }
}

Homework Equations





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.
 
Physics news on Phys.org
  • #2
What kind of error?

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

and

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

are not exactly identical.
 
  • #3
piece[p_count] is an attempt to access beyond the end of the array.

Try this:

Code:
    readPieces(piece, p_count);

...

void readPieces(PIECE *piece, int p_count){
 
  • #4
Borek said:
What kind of error?

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

and

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

are not exactly identical.

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
 
  • #5
rcgldr said:
piece[p_count] is an attempt to access beyond the end of the array.

Try this:

Code:
    readPieces(piece, p_count);

...

void readPieces(PIECE *piece, int p_count){

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.
 

1. How do I scan values into a struct in C?

In order to scan values into a struct in C, you need to use the scanf() function. The syntax for scanning values into a struct is scanf("%d %s %f", &myStruct.var1, myStruct.var2, &myStruct.var3); where var1, var2, and var3 are variables inside the struct and myStruct is the name of the struct.

2. Can I scan multiple values into a single variable within a struct?

Yes, you can scan multiple values into a single variable within a struct by using the appropriate format specifier in the scanf() function. For example, if you want to scan a string into a char array within a struct, you would use scanf("%s", myStruct.var);

3. How do I scan values into nested structs in C?

In order to scan values into nested structs in C, you can use the same syntax as scanning values into a regular struct. However, you will need to use the dot (.) operator to access the variables in the nested struct. For example, scanf("%d %s %f", &myStruct.nestedStruct.var1, myStruct.nestedStruct.var2, &myStruct.nestedStruct.var3);

4. What happens if I scan more values than the number of variables in my struct?

If you scan more values than the number of variables in your struct, the extra values will be ignored. However, this can lead to unexpected behavior and should be avoided.

5. How do I handle errors while scanning values into a struct in C?

To handle errors while scanning values into a struct in C, you can check the return value of the scanf() function. If it returns a value less than the number of format specifiers, it indicates that an error has occurred. You can also use the perror() function to print an error message to the console. Additionally, you can use the fflush() function to clear the input buffer in case of an error.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
3
Views
873
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
3
Views
745
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top