dynamic - C - Malloc char array access violation error -
dynamic - C - Malloc char array access violation error -
i'm not sure why malloc allocating much space. here's snippet of problem code:
char * hamming_string = null; void enter_params(){ printf("enter max length: "); scanf_s("%d", &max_length); hamming_string = (char *) malloc(max_length * sizeof(char)); // test what's going on hamming string for(int = 0; < strlen(hamming_string); i++){ hamming_string[i] = 'a'; } printf("hamming string = %s", hamming_string); }
i set max_length 2 , i'm seeing 12 a's. in function, going have user input hamming string using scanf_s("%s", &hamming_string); kept getting access violation
void check_code(){ int actual_length, parity_bit, error_bit = 0, c = 0, i, j, k; printf("enter hamming code: "); scanf_s("%s", &hamming_string); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
this scanf_s()
phone call incorrect.
according c11 documentation or msdn documentation needs be
scanf_s("%s", hamming_string, size - 1);
note don't know size
within function. note don't pass address of hamming_string
; hamming_string
gets converted address of first element.
c dynamic malloc
Comments
Post a Comment