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 11-13-2021, 11:14 AM   #1
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 28

Rep: Reputation: Disabled
Problem with a little piece of code in C


Hi,

I am new to C programming and I came across a problem. When I comiple and run the following code I get the output: You have entered +:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{


if ((*argv[1]) == '+'){


    printf("You have entered +");

}


    return 0;
}

But when I compile and run the following code I get not output at all:

Code:
#include <stdio.h>

int main(int argc, char *argv[])
{


if ((*argv[1]) == '*'){


    printf("You have entered *");

}


    return 0;
}
So what is the problem and how can I dissolve this?

Thanks.

Ben
 
Old 11-13-2021, 11:53 AM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to the Programming forum!

The problem is that when you enter '*' the shell expands that before it is sent to the program, so that *argv[1] is not a literal '*' but is the first value resulting from the expansion and the test fails.

To see what is actually there you might try adding an else clause to show the actual value pointed by argv[1] on failure.

When writing code and some test does not produce the expected result, a good first step for troubleshooting is to write in a way to see what the actual test parameters are when the program runs. Sometimes you may want to place that immediately before the test, and sometimes after or as an alternate when the test fails as I have suggested.

Good luck with your C programming!

Last edited by astrogeek; 11-13-2021 at 12:01 PM.
 
3 members found this post helpful.
Old 11-14-2021, 06:31 AM   #3
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 28

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Welcome to the Programming forum!
Thanks, and thanks for the help.

Ben
 
Old 12-06-2021, 01:41 AM   #4
SamHobbs
Member
 
Registered: Nov 2021
Location: California, USA
Posts: 83

Rep: Reputation: Disabled
When I execute:

Code:
if ((*argv[1]) == '+') {
	printf("You have entered +");
I get an access violation. And that makes sense because I did not provide an argument so there is nothing there to be accessed. It is instead trying to look at memory location zero. The fact that your code does not get an access violation is near proof that you are providing an argument somehow. We do not know what the argument is so the suggestion to see what is actually there was probably very helpful. If so then you should give credit for the help.
 
Old 12-06-2021, 07:13 AM   #5
FFX
LQ Newbie
 
Registered: Jul 2021
Distribution: Ubuntu
Posts: 28

Original Poster
Rep: Reputation: Disabled
Hi Sam,

My explanation was not complete. I did provide + as the first argument on the command line.
The same counts for the second code but then I provided *

Ben
 
  


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
piece of code Volcano Programming 8 08-04-2009 09:28 AM
is there a piece of c code that can set the terminal window tinieprotonjam Programming 2 01-27-2007 07:20 AM
building shell command piece by piece aunquarra Linux - General 7 05-02-2006 06:43 AM
Unusual piece of bash script code raypen Slackware 2 03-18-2006 10:49 PM
nice piece of code ....HOW DOES IT WORK? deadhead Programming 7 12-07-2003 12:52 AM

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

All times are GMT -5. The time now is 07:36 AM.

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