-
Using mallock in array
Hi, i'am trying to take info about student as an input and store it into a file, i got a probelm with taking a name, basically what i'am trying to do is allocate more memory to first_name[0]/last_name[0] so they can store string. Can you give a hand with what is the problem with a code? Thanks.
CODE:
Code:
char first_name[3];
char* ptr_first = &first_name[0];
ptr_first = (char*)malloc(100 * sizeof(char));
char last_name[3];
char* ptr_last = &last_name[0];
ptr_last = (char*)malloc(100 * sizeof(char));
float grade_test;
float grade_excercise;
char grade_final[5];
printf("First name: ");
scanf_s("%s", &ptr_first);
printf("Last name: ");
scanf_s("%c", &ptr_last);
printf("Grade-test: ");
scanf_s("%f", &grade_test);
printf("Grade-excercise: ");
scanf_s("%f", &grade_excercise);
printf("Grade-test: ");
scanf_s("%c", &grade_final);
FILE* ptr;
fopen_s(&ptr,"index.txt", "w");
fprintf(ptr, "[%s,%s,%f,%f]\n",*ptr_first,*ptr_last,grade_test,grade_excercise);
fclose(ptr);
-
Why not follow the KISS priciple?
Code:
#define NAME_LEN 100
char first_name[NAME_LEN];
scanf("%99s", first_name);
-
There is no such thing as extending an array.
Either you have an array (of fixed size), or you have dynamically allocated memory.
You do have to be careful with your use of & in scanf calls however.
Code:
char name[10];
scanf("%s",&name);
scanf("%s",name);
The first is wrong, but the result is the same either way. Whilst &name and name have different types, the actual address is the same.
Code:
char *name = malloc(10);
scanf("%s",&name);
scanf("%s",name);
The first is very definitely wrong, and in a bad way. scanf will trash your pointer, not make a string for you in the place you intended.
Finally, to save even more confusion, your "%c" calls should be written as " %c" (yes, a leading space) if your intention is to read the next non-space character.
-
Well, first thank you both for advice. Second i tried what thmm wrote but i'am getting same result, a the moment i write name and press Enter cmd gives me back this
Code:
(process 15916) exited with code -1073741819.
Press any key to close this window . . .
current code:
Code:
#include <iostream>
#define NAME_LEN 100
void add_student() {
char first_name[NAME_LEN];
char last_name[NAME_LEN];
float grade_test;
float grade_excercise;
char grade_final[5];
printf("First name: ");
scanf_s("%99s", first_name);
printf("Last name: ");
scanf_s("%99s", last_name);
printf("Grade-test: ");
scanf_s("%f", &grade_test);
printf("Grade-excercise: ");
scanf_s("%f", &grade_excercise);
printf("Grade-test: ");
scanf_s("%c", &grade_final);
FILE* ptr;
fopen_s(&ptr,"index.txt", "w");
fprintf(ptr, "[%s,%s,%f,%f]\n",first_name,last_name,grade_test,grade_excercise);
fclose(ptr);
}
-
Stop using scanf_s until you've read the manual.
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | Microsoft Docs
You have to pass additional parameters.
It's not a simple matter of just putting _s on the end of the name to achieve instant safety.
-
Thank you, it finally works.