Thread: Using mallock in array

  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    3

    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);
    Last edited by Vojta; 1 Week Ago at 07:02 AM.

  2. #2
    Registered User
    Join Date
    Sep 2020
    Posts
    39
    Why not follow the KISS priciple?

    Code:
    #define NAME_LEN 100
    char first_name[NAME_LEN];
    scanf("%99s", first_name);

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,302
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    3
    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);
    
    
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,302
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    3
    Thank you, it finally works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-22-2020, 12:42 PM
  2. Replies: 12
    Last Post: 07-31-2013, 12:15 AM
  3. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  4. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM

Tags for this Thread