Exception thrown at 0x7B7E1F4C (ucrtbased.dll) in MySOlExam.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD. occurred

-2

**Can someone tell me what is the issue i should fix. I omitted some of the code so that it doesn't get cluttered.The error is shown upon printing one of the struct elements in main.I have tried dynamic allocation for that and everything but nothing seems to work.Visual studio says that it is unable to read the memory field, which sounds stupid to me.Spent like a good 2 hours on this and no luck.New to C so i woul appreciate a detailed explanation on why **

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char* newString =(char*)malloc(sizeof(newString)) ;

typedef struct employee {
        char* pFirstname, * pMiddleName, * pLastName;
        int Brutto, TaxCoefficient, Netto;
    }EMPLOYEE;
EMPLOYEE* Exam(char*);
EMPLOYEE* Exam(char* pInput) {
    //char* str;
    char str[100];
    //str = strtok(pInput, " ");
    EMPLOYEE* JohnS = (EMPLOYEE*)malloc(sizeof(struct employee));
    int i = 0;
    int j = 0;
    int counter=0;
    char textline1[20][20];
    if (str[i] == ' ') 
    {
        textline1[counter][j] = NULL;
            counter++;
            j = 0;

    }
    else {
        textline1[counter][j] = str[i];
        j++;
        i++;
    }
    printf("\n Strings or words after split by space are :\n");
    for (i = 0; i < counter; i++)
    printf("%c\n", newString[i]);
    return JohnS;
}




int main() {
    char pInput[100];
    printf_s("Enter your name:");
    fgets(pInput,sizeof(pInput),stdin);
    //struct employee* = (char*)malloc(sizeof(employee));
    struct employee* John1 = Exam(pInput);
    //employee* JohnS = (employee*)malloc(sizeof(JohnS));
    printf("FirstName: %s\n", John1->pFirstname);
    printf("MiddleName: %s\n", John1->pMiddleName);
    printf("Lastname: %s\n", John1->pLastName);
    printf("TaxCoefficient: %d\n", John1->TaxCoefficient);
    printf("Brutto: %d\n", John1->Brutto);
    printf("Netto: %d\n", John1->Netto);
    return 0;
}
c++
exception
asked on Stack Overflow Jan 5, 2020 by Aleksandr Trohhatsov • edited Jan 5, 2020 by zig razor

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0