LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2010, 12:35 AM   #1
Reverse Logic
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Rep: Reputation: 0
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();

Does anyone have any ideas?
 
Old 01-23-2010, 12:41 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Can you show the code you are using to call the getnexttab function?
 
Old 01-23-2010, 12:51 AM   #3
Reverse Logic
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Code:
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
 
Old 01-23-2010, 01:08 AM   #4
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
It is odd, that which you are asking.

By looking at the following lines of code:

Code:
	printf("getnexttab() returning %d", index);
	return -1;
I would assume that you do not understand how the 'return' statement works.
 
0 members found this post helpful.
Old 01-23-2010, 01:15 AM   #5
Reverse Logic
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
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
 
Old 01-23-2010, 01:47 AM   #6
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Code:
	while ((currentindex = getnexttab(raw, currentindex, lengthofinput)) >= 0)
	{
		//replace tab with spaces here
	}
Until you actually do replace tabs with spaces, it's just going to keep finding the same tab forever.
 
0 members found this post helpful.
Old 01-23-2010, 01:55 AM   #7
Reverse Logic
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Until you actually do replace tabs with spaces, it's just going to keep finding the same tab forever.
Yes, I understand that.

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.
 
Old 01-23-2010, 02:27 AM   #8
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
1.
Code:
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:

Code:
	while ((currentindex = getnexttab(raw, currentindex, lengthofinput)) >= 0)
	{
		raw[currentindex] = ' ';
	}
 
Old 01-23-2010, 02:39 AM   #9
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Some more questions. I'm asking, because I'm not seeing the same symptoms as you.

What is the environment? (operating system, compiler, etc)

Do you get any warnings during the compilation?

What is the example data you are testing the program with? Is it a file or standard input? When it hangs, what is the output up to that point.?
 
Old 01-23-2010, 05:12 AM   #10
aspire1
Member
 
Registered: Dec 2008
Distribution: Ubuntu
Posts: 62

Rep: Reputation: 23
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 :)
 
Old 01-23-2010, 11:11 AM   #11
Reverse Logic
LQ Newbie
 
Registered: Jan 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Carbon Fiber:

1) You're absolutely right about the fprintf.

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.

Neonsignal:

1) I'm using fedora 12 64 bit. IDE: Fedora Eclipse Compiler: gcc 4.4.2

2) No. No warnings

3) Example data
Quote:
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.
 
Old 01-23-2010, 11:40 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Reverse Logic View Post
Yes, I understand that.

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:

Code:
fprintf(stderr, "index: %d\n", index);
 
Old 01-23-2010, 12:44 PM   #13
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
2 Reverse Logic
Did you try to use gdb or any other debugger? I'm sure you can find where your program hangs by tracing it.
 
Old 01-23-2010, 01:10 PM   #14
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, Reverse Logic -

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...
 
Old 01-23-2010, 02:20 PM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ problem setting "const char *str" from function returning a string ocularb0b Programming 3 03-04-2008 09:36 PM
converting char to unsigned integer in c mohtasham1983 Programming 2 02-27-2007 02:58 PM
C++ converting from char to integer MicahCarrick Programming 4 12-19-2005 02:16 PM
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
Comparing Char arrays n_ick2000 Programming 5 02-25-2003 09:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration