Scanning values to structs in C

  • Thread starter Thread starter mathmannn
  • Start date Start date
  • Tags Tags
    Scanning
Click For Summary
SUMMARY

The discussion focuses on the correct implementation of a function to read values into a struct in C, specifically for storing the dimensions of rectangles. The user initially encounters errors when attempting to pass an array of structs to a function. The solution involves changing the function signature to accept a pointer to the struct array and correcting the indexing method. The final working code successfully captures user input for length and width using the correct syntax.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with structs in C
  • Knowledge of function parameters and pointers in C
  • Basic input/output operations using scanf and printf
NEXT STEPS
  • Learn about pointers and memory management in C
  • Explore dynamic memory allocation using malloc and free
  • Study the use of arrays of structs in C programming
  • Investigate error handling techniques in C, particularly with user input
USEFUL FOR

Beginner C programmers, students learning about data structures, and anyone looking to improve their understanding of function calls and memory management in C.

mathmannn
Messages
15
Reaction score
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
What kind of error?

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

and

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

are not exactly identical.
 
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){
 
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
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K