Home > Enterprise >  Why doesn't my struct code doesn't work well?
Why doesn't my struct code doesn't work well?

Time:01-07

This is sample code.. But Code doesn't work... Here are some code and screenshot of my project. In this code, the result is

Location of RED BALL (3,4)

#include <stdio.h>
char copy_str(char *dest, char *src);
int Print_Obj_Status(struct obj OBJ);
 struct obj{
  char name[20];
  int x, y;
  } Ball;

int main(){
Ball.x = 3;
Ball.y = 4;
copy_str(Ball.name, "RED BALL");

Print_Obj_Status(Ball);

return 0;
 }

int Print_Obj_Status(struct obj OBJ) {
   printf("Location of %s \n", OBJ.name);
   printf("( %d, %d ) \n", OBJ.x, OBJ.y);

   return 0;
 }
char copy_str(char *dest, char *src){
while (*src){
    *dest = *src;
    src  ;
    dest  ;
 }

*dest = '\0';

return 1;
  }

enter image description here

CodePudding user response:

The function declaration int Print_Obj_Status(struct obj OBJ); declares struct obj only inside the function declaration. At the end of the declaration, that struct obj is no longer known for purposes of compiling later code. That is what the first message is warning you about.

Then this code:

struct obj{
  char name[20];
  int x, y;
  } Ball;

defines a new struct obj type. Because the previous struct obj is no longer known, this is a new type, even though it has the same name.

Later, when you call the function, in Print_Obj_Status(Ball);, you are passing it an object, Ball, of the later type struct obj, but the function was declared to take a different type, the earlier struct obj. Even though they have the same name, they were declared in different scopes and are different types. So the compiler gives you an error because the function is not being passed the type it was declared with.

The same problem happens with the function definition; int Print_Obj_Status(struct obj OBJ); the function is defined to have a parameter of the new type, which is different from the old type.

To fix this, put the definition of the structure first. Then, in the declaration int Print_Obj_Status(struct obj OBJ);, there is already a struct obj in scope. When struct obj appears in this form (without a following { that starts a structure definition) and there is already a declaration of struct obj in scope, it refers to the visible declaration instead of creating a new one. Then Print_Obj_Status will be declared to have a parameter of the same type as the type it is later called with and later defined with.

CodePudding user response:

Move the struct definition before the function definition:

struct obj{
    char name[20];
    int x, y;
} Ball;

int Print_Obj_Status(struct obj OBJ);

CodePudding user response:

The compiler complains because struct obj is not yet declared when it reaches the declaration of Print_Obj_Status. This is easily solved by placing the struct declaration before the function declaration. Also, the function doesn't need to return a value and you don't need to declare your own string copy function. If you include the header string.h you can use strcpy:

#include <stdio.h>
#include <string.h>

struct obj {
    char name[20];
    int x, y;
} Ball;

void Print_Obj_Status(struct obj OBJ)
{
    printf("Location of %s \n", OBJ.name);
    printf("( %d, %d ) \n", OBJ.x, OBJ.y);
}

int main(void)
{
    Ball.x = 3;
    Ball.y = 4;
    strcpy(Ball.name, "RED BALL");
    Print_Obj_Status(Ball);
    return 0;
}
  •  Tags:  
  • c
  • Related