#Brand new C programmer V confused abt SegFault

36 messages · Page 1 of 1 (latest)

normal scarab
#

I just started learning C and I keep coming across Segmentation Fault, something I am glad I haven't had to deal with in other languages, but here I am. I am trying my best to look into SegFault online but I will be honest, I can barely understand it. (This might also come from my lack of knowledge on pointers that I am trying to refine) If anyone can point out where exactly the problem might be, and exactly why it's a problem that would be amazing. I have spent way too long over this and at this point I am getting burnt out but I don't want to just give up so I came here.

Also I am currently waiting to hear back from JetBrains to get clion working for me so atm I am using the Git Bash shell which imo looks terrible, I apologize.

idle ermineBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

manic coyote
#

I'd recommend giving address sanitizer a try

#

For gcc/clang on linux it's as simple as -fsanitize=address

spare belfry
#

you can't compare strings like that

manic coyote
#

Otherwise if you run in the debugger you can get information about what line the segfault occurs on and information about the values of variables at that point

spare belfry
#

it just compares the values of the pointers

normal scarab
#

wouldn't you use strcmp?

#

strcmp(givenStr, "\0") != 0?

#

I think

manic coyote
#

Some of the naming here is potentially misleading

#

Is givenChar a single character?

normal scarab
#

lol

manic coyote
#

If so, it can be a regular old char

normal scarab
#

you sound like a friend of mine, and it can be

#

I know it can be misleading, but for this it works for me at the moment

#

havent spent the time to name things properly😅

manic coyote
#

All good, it'd just also simplify some of the logic

normal scarab
#

how so?

manic coyote
#

e.g. givenStr += strlen(a single char); -> givenStr++;

normal scarab
#

true

manic coyote
#

Instead of strcmp(givenStr, "\0") != 0 I'd just write *givenStr != '\0'

#

similarly strstr wouldn't be needed

normal scarab
#

wait

#

U know what, you are def correct

#

not enough energy to even think about that rn tho sorry

manic coyote
#

all good

#

there's always a dozen ways to do things and always feel free to do whatever you want, but I would be remiss to not mention an alternative approach

normal scarab
#

thank you for that

#

its always nice to have another opinion on things

#

now time to learn linux

#

and debugging

#

!solved

idle ermineBOT
#

Thank you and let us know if you have any more questions!

#

[SOLVED] Brand new C programmer V confused abt SegFault