Thread: Using mallock in array

Threaded View

Previous Post Previous Post   Next Post Next Post
  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; 11-22-2020 at 07:02 AM.

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