'fscanf' changing pointer to struct in C

Asked

Viewed 79 times

0

Good night.

I was writing a program to read strings from a text file, and put these on a list simply chained in case its length was equal to 5.

The problem is that the program started to not work the way I did waited, and after a few tests, I discovered that a pointer I was on using would point from nowhere to another memory address, after a few iterations and a fscanf command'.

I put in Pastebin the test code I wrote: https://pastebin.com/4VRrxqgJ

The "Keys.txt" file contains the following strings:

m4mSx
fr4
mWKgC
7bbak
7
Np3rw

I wrote another code that solves without problems what I wanted to do, but i would like to know why is happening this mistake with this test code, since from what I know it doesn’t make sense that this is occurring.

  • 1

    Do not forget that strings in C are terminated by ' 0' so to save "m4mSx" you will need at least 6 boxes.

  • I tested your program and there was no change of address... The program you passed was edited, right? Could you provide the original? I don’t see any problems other than what @Jjoao mentioned and the fact that you did type-cast us mallocs.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.