strtok and strcmp pointer issues question

This is a discussion on strtok and strcmp pointer issues question within the C Programming forums, part of the General Programming Boards category; Hi everyone,
I want to tokenise a text file with around 20 lines, and as the lines could be moved ...

strtok and strcmp pointer issues question

Hi everyone,
I want to tokenise a text file with around 20 lines, and as the lines could be moved around, I want it to check the tokens for a certain word and if it contains that word, I want to equate the second token to it.

Been playing with it some more and thought I'd tried this already but obviously not - changing the single quotes to double quotes in strcmp it now compiles fine but comes up with a segmentation fault when run. Any idea?

Arrays start at zero, not one, so you should be starting your loop with i=0. You never check to see if mass is not-NULL before you use it. You pretty much don't do that ever. You should really be doing two things: (1) makes sure you initialize your pointers before using them, (2) check to see they're not-null before trying to dereference them.

Thanks for the quick replies. Ok, changed the loop and added a check for NULL.

I'm not completely sure what you mean by initialising the pointers - a quick search of the internet says after declaring them I just need to do e.g. mass = some kind of string, which is what should be happening inside my strtok loop, but just to be sure I've done:

Like I said, you need to make sure your functions are doing what you expect before you start blindly using the arrays you expect to have stuff in them. Also pay attention to the last line of output you actually see, that'll give you a good indicator of where things are gone wrong.

You don't need the brackets round the 'if' statement if there's only one line involved.. only learnt that yesterday from my project supervisor, this is just one part of a program that's over 1000 lines and space-saving is also effort-saving when I need to find particular sections.

It's not able to extract 'mass' so the strcmp part isn't working for whatever reason - 'mass', 'WINGSPAN' and 's' all come up as (null) when printed within the strtok loop.

It seg faults right at the end of the strtok loop, without doing the atofs.

You don't need the brackets round the 'if' statement if there's only one line involved.. only learnt that yesterday from my project supervisor, this is just one part of a program that's over 1000 lines and space-saving is also effort-saving when I need to find particular sections.

Yes, it is true that if you leave out the braces, you may be able to shorten your program in terms of number of lines, but this does not necessarily make it easier to find particular sections, and in fact if you try and "squeeze" your source code, you may find it even more difficult to read and hence to find anything at a glance. Furthermore, because indentation, however important, is not what the compiler actually uses to determine scope, keeping "superfluous" braces can be useful to avoid bugs that may arise during maintenance.

A point that you want to take note: programs that are "over 1000 lines" are actually quite common. I noticed that your sample code includes none of your own headers and comes with no other function declarations. If you want to be able to handle larger programs, then you should start defining other functions that do one thing and do it well (in separate source files, if necessary).

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

You completely missed the point I was making. You need to check to see that your variables have valid data before you start fiddling with them. Way to miss the obvious and distract yourself because I happen to have included a pair of braces. I suspect you'll be working on this project for a very long time.

I thought since everyone on this forum has been helpful I would try and help back - I didn't know about the braces before yesterday - sorry if I insulted your intelligence.

I -did- check the variables and there's nothing in them, meaning strcmp isn't working in the way I thought it was, and so I wanted to know if there was another way of doing it.. oh well. Thanks anyway. Not everyone's been doing this for years.

Well, what is arra? The first place where it appears in your code snippet, it is used in strtok, which implies that it must have been used earlier, otherwise it would be an example of the kind of mistake that quzah is talking about.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Oh, apologies. arra is the lines of the file. So arra[1] is line 1, arra[2] is line 2. That bit works alright definitely, because printing my tokens works fine, it's just the comparing and equating section that's not.