I've the following string saved into a char array named buffer
:
{app_id:9401}
I want to obtain two integers: 94 and 01. I want to convert buffer[8]
and buffer[9]
into the first integer(94
) and buffer[10]
and buffer[11]
into the second integer (1
). I'm trying to do it, but I obtain segmentation fault. Why?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
char buffer[] = "{app_id:9401}";
printf("First number:%c%c. Second Number: %c%c\n", buffer[8], buffer[9], buffer[10], buffer[11]);
char first_number[10] = "";
strcat(first_number, buffer[8]);
strcat(first_number, buffer[9]);
int x = atoi(first_number);
printf("%d\n", x);
}
CodePudding user response:
Here is one way, by example. Scan two integers from the string, each with a maximum length of 2.
#include <stdio.h>
int main(void)
{
char buffer[] = "{app_id:9401}";
int x, y;
if(sscanf(buffer 8, "--", &x, &y) == 2)
printf ("x=%d y=%d\n", x, y);
return 0;
}
Output:
x=94 y=1
If you don't know where the number is in the string, you can find the colon with
char *cptr = strchr(buffer, ':');
CodePudding user response:
[...] but I obtain segmentation fault. Why?
The function strcat
requires both function arguments to be pointers to strings. However, you are not providing a pointer as a function argument. Instead, you are passing the value of a single character. Any good compiler should warn you about this, provided that you have warnings enabled. I suggest that you ensure that you always do have them enabled. See this question for further information:
Why should I always enable compiler warnings?
This invalid function argument is probably the reason for your segmentation fault.
Using the function strcat
is probably not what you want, because you want to concatenate a single character to a string, instead of a string to a string. The C standard library does not provide a function which does this. However, you can easily implement such a function yourself:
void concatentate_string_with_character( char *dest, char c )
{
//calculate length of destination string
size_t length = strlen( dest );
//overwrite terminating null character with new character
dest[length] = c;
//write new terminating null character
dest[length 1] = '\0';
}
If you replace in your code the function strcat
with concatentate_string_with_character
, then it will work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void concatentate_string_with_character( char *dest, char c )
{
//calculate length of destination string
size_t length = strlen( dest );
//overwrite terminating null character with new character
dest[length] = c;
//write new terminating null character
dest[length 1] = '\0';
}
int main( void )
{
char buffer[] = "{app_id:9401}";
char first_number[10] = "";
concatentate_string_with_character( first_number, buffer[8] );
concatentate_string_with_character( first_number, buffer[9] );
int x = atoi( first_number );
printf( "%d\n", x );
}
This program has the correct output 94
.
However, instead of using a separate function for this, it would probably be easier to simply define the three elements of first_number
yourself:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char buffer[] = "{app_id:9401}";
char first_number[] = { buffer[8], buffer[9], '\0' };
int x = atoi( first_number );
printf( "%d\n", x );
}
This program also has the correct output 94
.