Access violation during dynamic assignment of C language two-dimensional array

Asked 2 years ago, Updated 2 years ago, 81 views

DYNAMIC ASSIGNMENT OF TWO-

Exception occurred (0x00F55146, Project1.exe): 0xC0000005: Access violation occurred while reading location 0xFDFE01

You have received the following message:

#include<stdio.h>
#include<stdlib.h>

int main() {

    int row, column;

    scanf_s("%d %d", &row, &column);

    char** matrix = (char**)malloc(sizeof(char*)*row);

    for (int i = 0; i < row; i++)
        matrix[i] = (char*)malloc(sizeof(char)*column);

    matrix[0][0] = 'a';

    printf("%c", matrix[0][0]);

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < column; j++)
            scanf_s ("%c", matrix[row][column], 1); // access violation location
    }

    for (int i = 0; i < row; i++) {
        printf("\n");
        for (int j = 0; j < column; j++)
            printf("%c ", matrix[column][row]);
    }
}

As you can see, the matrix[0][0] has no problem with adding the letter a.

From this result, I understood that the memory allocation at matrix[0][0] became normal.

An access violation occurred when attempting to assign matrix[0][0] by reading characters with scanf_s.

Access violations occur when you try to allocate data where there is no memory space.

The same was true for scanf_s without assigning the letter a to matrix[0][0] TT

Why is that?

2d-array memory-allocation accessibility

2022-09-22 13:07

1 Answers

scanf_s(" %c", &(matrix[i][j]), 1);


2022-09-22 13:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.