LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sscanf strangeness (https://www.linuxquestions.org/questions/programming-9/sscanf-strangeness-805564/)

exvor 05-02-2010 08:03 PM

sscanf strangeness
 
Hello I am trying to compile a small program in C and I am a bit perplexed as to why I am getting a gcc warning to recast this.

the program uses this

sscanf(argv[1],"%i%c%i",&option1,&throwaway,&option2);

I do this to try and capture only the integers in the string I really don't care about the char in between. Is sscanf looking for a diffrent kind of variable to put the char into because I declared throwaway as char? Gcc keeps thinking im putting it into a integer?

Sergei Steshenko 05-02-2010 08:11 PM

Quote:

Originally Posted by exvor (Post 3955131)
Hello I am trying to compile a small program in C and I am a bit perplexed as to why I am getting a gcc warning to recast this.

the program uses this

sscanf(argv[1],"%i%c%i",&option1,&throwaway,&option2);

I do this to try and capture only the integers in the string I really don't care about the char in between. Is sscanf looking for a diffrent kind of variable to put the char into because I declared throwaway as char? Gcc keeps thinking im putting it into a integer?

Publish your full source code and the exact command line you used to compile as well as the exact text of warnings.

...

From the manpage:

Code:

      c      Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to  char,  and  there
              must  be enough room for all the characters (no terminating null byte is added).  The usual skip of leading white space is suppressed.  To skip white space
              first, use an explicit space in the format.

.

Do you expect just one character between the two integers ?

exvor 05-02-2010 08:22 PM

Code:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, const char* argv[])
{
  int optionone = 0;
  int optiontwo = 0;
  char throwaway = NULL;


  sscanf(argv[1],"%i%c%i",&optionone,&throwaway,&optiontwo);
 
  printf("The options are %i and %i",optionone,optiontwo);

 return 0;
}

This is just a test program to see how to do it effectively really, like I mentioned before I would rather just ignore the charecter inbetween the integers.

This is how it was compiled
Code:


treah@darknode$ gcc -Wall test.c -o test
test.c: In function 'main':
test.c:9: warning: initialization makes integer from pointer without a cast

The program works ok just get this warning for some reason. The way im using the program is ./test 800x600

Sergei Steshenko 05-02-2010 08:35 PM

Quote:

Originally Posted by exvor (Post 3955147)
Code:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, const char* argv[])
{
  int optionone = 0;
  int optiontwo = 0;
  char throwaway = NULL;


  sscanf(argv[1],"%i%c%i",&optionone,&throwaway,&optiontwo);
 
  printf("The options are %i and %i",optionone,optiontwo);

 return 0;
}


This is just a test program to see how to do it effectively really, like I mentioned before I would rather just ignore the charecter inbetween the integers.

This is how it was compiled
Code:


treah@darknode$ gcc -Wall test.c -o test
test.c: In function 'main':
test.c:9: warning: initialization makes integer from pointer without a cast

The program works ok just get this warning for some reason. The way im using the program is ./test 800x600

I think 'gcc' complains about a different thing - try to replace

Code:

char throwaway = NULL;
with

Code:

char throwaway;

exvor 05-02-2010 08:39 PM

Yeah that's what it is. Hmmm I guess null is considered a integer. Sorry if this seams like a stupid question, im just getting back into c programing again.

Sergei Steshenko 05-02-2010 08:41 PM

Quote:

Originally Posted by exvor (Post 3955167)
Yeah that's what it is. Hmmm I guess null is considered a integer. Sorry if this seams like a stupid question, im just getting back into c programing again.

It's the other way round - IIRC NULL is

Code:

(void *)0
.

smeezekitty 05-02-2010 08:52 PM

Use zero instead of null or type (int)NULL.

Sergei Steshenko 05-02-2010 09:18 PM

Quote:

Originally Posted by smeezekitty (Post 3955182)
Use zero instead of null or type (int)NULL.

There is no need to initialize in the first place.

exvor 05-02-2010 09:38 PM

I thought you were supposed to initialize any variable because they can contain random data from where ever the memory was allocated from. The only way to be able to test for a condition is if you know exactly what will be there.

Sergei Steshenko 05-02-2010 09:43 PM

Quote:

Originally Posted by exvor (Post 3955205)
I thought you were supposed to initialize any variable because they can contain random data from where ever the memory was allocated from. The only way to be able to test for a condition is if you know exactly what will be there.

You are not checking the value which is to be thrown away, are you ?

Generally speaking, you need to initialize only those variables, whose value is going to be used before it comes "from outside" or is calculated.

ArthurSittler 05-04-2010 05:45 AM

I would be afraid that sscanf might see more than one char and overflow your throwaway memory. I would actually allocate a buffer and pass the address of the buffer:

#include<stdio.h>
#include<stdlib.h>

int main(int argc, const char* argv[])
{
int optionone = 0;
int optiontwo = 0;
char throwaway[20];


sscanf(argv[1],"%i%c%i",&optionone,throwaway,&optiontwo);

printf("The options are %i and %i",optionone,optiontwo);

return 0;
}

Because of the way C handles arrays and pointers, the address of the buffer as an array of char is referenced by the variable name or equivalently the address of the first element of the array, so I removed the unary prefix & from the modified code. I believe C would also accept &throwaway[0] as that parameter.

I believe the correct way to initialize a single char variable would be a null char in single quotes:

char throwaway = '\0';

If I allocated a buffer and wanted to initialize it, I would try

char throwaway[20] = "";

All this is hypothetical, though. I did not actually compile and test it.

ntubski 05-04-2010 08:16 AM

scanf(3)

Quote:

An optional '*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments returned by scanf().


All times are GMT -5. The time now is 10:07 PM.