Scanning values to structs in C

  • Thread starter Thread starter mathmannn
  • Start date Start date
  • Tags Tags
    Scanning
AI Thread Summary
The discussion focuses on how to properly use structs in C to store user input for the dimensions of rectangles. The original poster successfully implemented a program without functions but encountered issues when attempting to separate the input logic into a function. Key errors included incorrect function parameter passing and accessing array elements beyond their bounds. The solution involved changing the function call to accept the struct array correctly and ensuring that the input was stored in the correct struct members. Understanding these adjustments resolved the issues, highlighting the importance of proper array handling in C programming.
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
Views
10K
Replies
3
Views
1K
Replies
3
Views
1K
Replies
12
Views
2K
Replies
4
Views
1K
Replies
12
Views
2K
Replies
3
Views
2K
Replies
3
Views
1K
Back
Top