C function not returning an integer after comparing it to a char
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C function not returning an integer after comparing it to a char
I've got a strange bug that I cannot figure out how to solve. Here is the function.
Code:
int getnexttab(char input[], int index, int length)
{
while (index < length)
{
printf("value of index: %d.\nvalue of length: %d\n", index, length);
if (input[index] == '\t')
{
printf("We have a match\n");
return index;
}
index++;
}
return -1;
}
I'm trying to return the index of the first tab character found in an array of characters, or -1 if it is not found. Everything works perfectly until the code gets to the if statement. It will print the "We have a match" line, but as soon as I try to do anything with index after getting a positive comparison, the code will just hang. Even if all I'm doing is trying to print it with printf();
int main(void)
{
char raw[MAXFILELENGTH];
int lengthofinput;
int currentindex = 0;
lengthofinput = getinput(raw);
if (lengthofinput < 0)
return EXIT_FAILURE;
printf(raw);
while ((currentindex = getnexttab(raw, currentindex, lengthofinput)) >= 0)
{
//replace tab with spaces here
}
return EXIT_SUCCESS;
}
int getinput(char input[])
{
int c;
int i = 0;
while( (c=getchar()) != EOF)
{
if (i > MAXFILELENGTH)
{
sprintf(stderr, "Error: input file is larger than MAXFILELENGTH.\n");
return -1;
}
input[i] = c;
i++;
}
i++;
return i;
}
int getnexttab(char input[], int index, int length)
{
while (index < length)
{
printf("value of index: %d.\nvalue of length: %d\n", index, length);
if (input[index] == '\t')
{
printf("We have a match\n");
return index;
}
index++;
}
printf("getnexttab() returning %d", index);
return -1;
}
I've done a little C++ in the past, but right now I'm doing exercise 1-20 in a book called "The C Programming Language" by Brian Kernighan and Dennis Ritchie if that helps anyone. .
Last edited by Reverse Logic; 01-23-2010 at 12:52 AM.
Reason: Fixing Tags
I know how the return statement works. I had originally designed the function another way, and wanted to check the value that I was returning before it returned, but left it in there so I could see that it was indeed getting passed the while loop.
Regardless, that still doesn't have anything to do with the fact that I can't use the index variable after the if statement has a positive match.
Last edited by Reverse Logic; 01-23-2010 at 01:18 AM.
Reason: incorrect variable name
sprintf(stderr, "Error: input file is larger than MAXFILELENGTH.\n");
I believe you are looking for fprintf.
2. Your code is scattered across various posts. It would have been best to present your whole program in one post.
3. As it is, it would seem that the program does work (i.e. "function"). As noted in an earlier post, you are dealing with an infinite loop, which would case "We have a match..." to be written to stdout over and over again. Try the following: "anything else\n", "index: %d\n"; fflush(), and mainly:
It looks like if you find a '\t' then the value of index is never changed and you get stuck in an infinite loop.
You find a '\t' at position 1 so you return 1.
CurrentIndex now equals 1 which is >=0 so you call gettnextab again which fines a '\t' at position 1 and so on....
As was said before
Last edited by aspire1; 01-23-2010 at 05:15 AM.
Reason: Not reading the other posts properly :)
2) All of my code is posted in my second post, with the exception of my #includes and #defines.
3) No, it's not an infinite loop. "We have a match" only prints once, and the program hangs. The function does not return. it just stops working whenever I try to use the 'index' variable in any fashion.
How are you\tGentleman. All\tyour base are\tbelong to us.
This data is created using gedit, and to execute my program I type: cat test | ./detab, where test is the name of the text file, and detab is the name of my program.
aspire1:
1) Yes, you're right. I was going to increment currentindex in that while loop after I replaced the tab character. But the code doesn't get to that point. Again, please see my first post with the issue I am having. It's not stuck in a loop right now. It just hangs.
This still does not address the fact that I cannot use the 'index' variable.
Maybe I need to clarify.
Code:
if (input[index] == '\t')
{
printf("We have a match\n"); //fine
printf("anything else"); //fine
printf("index: %d", index); //fail
return index; //fail
}
If I do anything involving the 'index' variable, it causes the program to hang.
Item in bold lacks "\n"; at all, it's a bad idea to use stdout for diagnostic output because of buffering, a much healthier approach is to use stderr, i.e. rewrite your diagnostic messages like this:
You're getting lots of good advice here - please listen to some of it.
You *are* getting an infinite loop (it's easy to see where). You *should* be using a debugger (I assume that's practical; of course, it might not be, depending on your circumstances). And you *should* use "fprintf (stderr, ...)" (in order to make sure our output is shown *immediately*, and not "buffered"). With "printf()", you might *never* get to see your output (because the output is still buffered when the hang occurs).
There are other problems with the code, too.
Below is a "cleaned up" version (with most problems still in-place), and sample output.
'Hope that helps .. PSM
Code:
#include <stdio.h>
#define MAXFILELENGTH 255
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
int
getinput(char input[])
{
int c;
int i = 0;
while( (c=getchar()) != EOF)
{
if (i > MAXFILELENGTH)
{
fprintf(stderr, "Error: input file (%d) is larger than MAXFILELENGTH.\n", i);
return -1;
}
input[i] = c;
i++;
}
i++;
return i;
}
int
getnexttab(char input[], int index, int length)
{
while (index < length)
{
fprintf(stderr, "value of index: %d.\nvalue of length: %d\n", index, length);
if (input[index] == '\t')
{
fprintf(stderr, "We have a match\n");
return index;
}
index++;
}
fprintf(stderr, "getnexttab() returning %d", index);
return -1;
}
int
main(int c, char *argv[])
{
char raw[MAXFILELENGTH];
int lengthofinput;
int currentindex = 0;
lengthofinput = getinput(raw);
if (lengthofinput < 0)
return EXIT_FAILURE;
fprintf(stderr, "%s\n", raw);
fprintf (stderr, "currentindex: %d...\n", currentindex);
while ((currentindex = getnexttab(raw, currentindex, lengthofinput)) >= 0)
{
fprintf (stderr, "currentindex: %d...\n", currentindex);
}
return EXIT_SUCCESS;
}
Quote:
ABC<tab>def
^Z
ABC def
╠╠╠╠╠╠╠
<= Notice that the input string was never terminated...
currentindex: 0...
value of index: 0.
value of length: 9
value of index: 1.
value of length: 9
value of index: 2.
value of length: 9
value of index: 3.
value of length: 9
<= Cool! We found the tab (index= 3) and we have a length (9)
I'm not sure "9" is necessarily the value you wanted, but...
We have a match
currentindex: 3...
<= We have our match and our index.
We can do whatever we want with them
value of index: 3.
value of length: 9
We have a match
currentindex: 3...
<= Over...
value of index: 3.
value of length: 9
We have a match
currentindex: 3...
<= And over...
value of index: 3.
value of length: 9
We have a match
<= And over again...
And do remember that C arguments are passed by value, not reference. This means that changing the value of "index" inside your function will only change the value in the call stack, not the value in the calling program.
Last edited by PTrenholme; 01-23-2010 at 05:01 PM.
Reason: Extra "not" in sentence
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.