C structure pointer alignment segmentation fault question

Asked 2 years ago, Updated 2 years ago, 100 views

A function that aligns the structure pointer called Record *record[]. But I don't see any wrong use of memory, so I'm asking you this question. Why does the segmentation fault appear here? All other functions are functioning properly, but only this function shows segmentation fault... Help me

c struct-pointer sorting

2022-09-21 11:10

2 Answers

#include <stdio.h>>
int main(void)
{
    int a[5] = {1,2,3,4,5};
    int i,j;
    for(j=0; j<5; j++) 
    {
        printf("%d\n",a[j+1]);
    }
}
//Results
2
3
4
5
0 // overflow!!

In my opinion, like the answer above, within the double for statement

When referring to records [j+1], overflow occurred for the content.

I think segmentation fault has occurred.

#include <stdio.h>>
int main(void)
{
    int *a[5] = {1,2,3,4,5};
    int i,j;
    for(j=0; j<5; j++) 
    {
        *a[j+1]='3';
    }
}

//signal: segmentation fault (core dumped)


2022-09-21 11:10

I think Kim Ho-won's point is a problem, and the more problematic part is the lower part.

for( j = 0; i<count ; j++)

Here, j will increase indefinitely.

If you run this part in debug mode and look closely at the local variable values at the time of death, it seems that it will be solved alone. In fact, the most obvious cause of the program's death is the problem.


2022-09-21 11:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.