[C language] segmentation problem with creation of core dump
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You're application has a bug, that is probably causing it to crash here:
Code:
...
printf("Nome: ");
scanf("%s", n);
You should revisit your C book regarding function, and how to pass parameters to such. In your program example, I do not see a need to pass any parameters to the function. After all, you are not using any of them upon returning from the function.
Thus I recommend that you declare your variables local to the function, and avoid global variables. As for the crash, you need to declare space for the name variable 'n'. All you have is an uninitialized pointer that is referencing "somewhere" in memory.
Last edited by dwhitney67; 05-19-2013 at 07:02 AM.
Functions are great for performing the necessary steps of a compartmentalized task. It would seem from the code you have written thus far that the function lettura_studente() is meant merely to capture information about a student within a 'studente' record.
The problem with the function above is that you have a declared all parameters as local variables, and these will NOT be visible when the function returns.
Using psionl0 advice, fix your studente structure to pre-allocate memory for the char arrays. Then, all you would need to do is pass the address of a studente object to the function.
And, now for deeper advice... never read input from a user using scanf(). Use fgets(), and if a number is needed, then convert it from the string that is inputted. A user is prone to making input errors, sometimes by mistake, other times intentionally.
An example of using fgets()...
Code:
char buffer[80];
int number = -1;
if (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
char* newline = strchr(buffer, '\n'); /* check if a newline character is in the buffer */
if (newline != NULL)
{
*newline = '\0'; /* replace the newline with a null-character to mark the end of the string */
}
/* to convert input to a number */
int success = sscanf(buffer, "%d", &number);
if (success != 1) /* we are expecting only one conversion */
{
fprintf(stderr, "Failed to read number.\n");
}
}
else
{
fprintf(stderr, "Failed to read.\n");
}
In the example above, you can substitute 'buffer' with your char array (e.g. nome or cognome) if all you care about is a string. When using sscanf(), substitute 'number' with your variable (e.g. num_esami).
Yes, my problem is that to copy a string defined as "char*" into another string defined in the same way.
The biggest problem is that C doesn't really have a "string" data type.
C has many functions and conventions that treat an array of char as if it were a string. But since it isn't a real string type, there are lots of details in its use that are confusing to beginners.
Your "char*" variables are not arrays of char, so they are another step removed from being "strings". I'm sure you have seen examples in which a "char*" variable is successfully treated as if it were a string. But it is not inherently a string. You need to cover a lot of details in order to successfully pretend a "char*" is a string.
Any pointer in C might point to a single instance of whatever kind of thing it is declared to point to, or it might point to an array of them. That is another aspect of C that confuses most beginners. There is no difference in declaration between a pointer to a char and a pointer to an array of char.
A pointer in C doesn't point to anything until you set it to point to something. Making a "char*" variable means you have specified what kind of thing it points to, but you haven't made it point to any specific thing. Then you made the common beginner mistake of using the pointer to set the contents of the thing it points to, before setting the pointer to point to any specific place.
Your next big problem area is that you are using an input to a function as the mechanism for the function to deliver results. Maybe you have seen that done in some other language where objects are passed "by reference". In C, object are passed "by value", meaning they are copied as input to a function and discarded when the function is done. To get around that, you need to insert an extra level of indirection:
The & where you pass a struct to a function tells it to pass the address of struct rather than the value. The * where the function receives the struct tells it to receive the address rather than the value. The -> instead of . where you access a field of the struct tells it to access by address.
Quote:
Originally Posted by psionl0
With a suitable size for STRING_LENGTH you could try
Code:
char nome[STRING_LENGTH];
I agree. The original uses a "char*" instead of a char array for use instead of a string. Since strings don't actually exist, you need a char array instead of a string. Sometime you will need to use a "char*" as a pointer to a char array. But don't confuse yourself by using a "char*" instead of a char array. When you use a char*, the char array must also exist, it is just somewhere else. If the char array has no need to be somewhere else, then using a char* just creates extra work (which you then neglected to do). Keep it simpler and use a char array when that is all that is needed.
Quote:
Originally Posted by dwhitney67
void lettura_studente(studente& s);
That looks like you forgot you are answering a C question, not C++. That syntax in C++ changes the "pass by value" into a "pass by reference", so the rest of the uses of that variable (which were depending on pass by reference) could be left unchanged.
But C doesn't have pass by reference. Instead you need to pass the address. That requires changes in more places (as I showed above).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.