Scanning values to structs in C

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

Discussion Overview

The discussion revolves around the use of structs in C programming, specifically focusing on how to properly pass an array of structs to a function for inputting values. Participants are exploring issues related to function calls, array indexing, and variable access within structs.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares their code for storing the dimensions of rectangles using structs and expresses confusion about moving input functionality into a separate function.
  • Another participant points out that there is a difference between accessing the struct members using "length" and "len," suggesting that this could be a source of error.
  • A participant highlights that the original function call attempts to access an array index that is out of bounds, recommending a corrected function call.
  • There is a request for clarification on the concept of accessing beyond the end of an array, indicating a lack of understanding about how array indexing works in relation to user-defined input.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to resolve the issues presented, as there are multiple suggestions and corrections offered without definitive agreement on a single solution.

Contextual Notes

Limitations include potential misunderstandings about array indexing and function parameter passing in C, as well as the need for clarity on the differences between struct member names.

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
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K