||=== Build: Debug in New_Project1 (compiler: GNU GCC Compiler) ===|
In function 'newUser':|
warning: assignment makes integer from pointer without a cast [-Wint-conversion]|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
You can't use [] in a struct
in fact I did use it:
No you didn't. You used [50] etc where the size was specified. [] means that the size will be determined and not pre-stipulated. There is a difference.
You can certainly use [50] etc within a struct as the size is specified at compile time.
Btw. I tried const char *const name = "user.txt"; but it gives me an error:
main.c|39|error: expected ':', ',', ';', '}' or '__attribute__' before '=' token|
Ohh.. I guess I have to apologize for that,..sorry I thought [n] and [] are the same thing..and that compiler is treating both the same either if the size is determined or not.
Ohh.. but I don't understand why I get this error.. I reedit the code again and now I get another error in the void newUser(); function 3rd line FILE* file = fopen(info.name, "r+"); says:
What language are you using to compile this code, C or C++? It appears to be C so assigning values in the structure definition is not allowed.
If you're trying to write C++ code you should start using C++ features, like std::string, std::streams, etc, and stop using C features like FILE, C-strings, etc.
No matter what language you are attempting to use you should stop using global variables, ie: info is a global variable in your code.
Okay.. atm. I'm a beginner and I start to learning C, maybe later I'll pass to C++, but for now I'll stik with it and if you say so, then it's useless what I'm trying to do, I'll never be able to do that with a structure. I'm going to use a global variable JUST ONE
const char name[MAXNAME] = "user.txt";
, because I use it in more then 3 functions.. and yes I'm trying to keep distance from global as much as I can, but as I said I do not have that experience and I'm trying to learn something new from all of you.
Thank you everyone.. I really appreciate everyone's help.. until the next matter Cheers !! :)
Well I did create a snake game my last post was about that and now I was thinking to add in the game something like a premenu when the user should add a name and the name is saved on a file, after entering the game can be played and it has a score too so I decide to make a structure where it should be save both the name and the score... but since in 'C' I can't assign values in the structures definition I can only create 2 global variables for name and score, and to go on.
About my last post:
This code compiles without no errors and warnings, the only problem is that what I did is useless as
info.name[MAXNAME] = *fname
doesn't assign info.name the "user.txt", so yes to make this work strcpy may be a solution, I did not try it yet as I was so exited to see if it works with the global variables, but if I fail I'll try your solution jlb
Part of the problem with that assignment is that fname is a local variable, which means that it will go out of scope when the function ends. Another part of the problem is the [MAXNAME] after the variable in that assignment, you're confusing the compiler with "invalid" syntax.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXNAME 100
struct Info
{
int score;
char name[MAXNAME];
};
void newUser(struct Info* info);
int main()
{
// Avoid global variables, learn to properly pass variables to and from your functions.
struct Info info;
newUser(&info);
printf("%s\n", info.name);
}
// Learn to properly pass variables to and from your functions as required.
void newUser(struct Info* info)
{
// info is a pointer so you must use pointer notation.
strcpy(info->name, "user.txt");
printf("%s\n\n", info->name);
}
Edit: Also if you're working on a C program you should mention that when you create the topic, as most people will assume that you are working on a C++ program, since this is a C++ forum.