1

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);
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • C specifies that all digit characters should be consecutively encoded, which means you can subtract `'0'` from any digit character to get its integer equivalent (i.e. `'9' - '0' == 9`). Then iit's just a matter of decimal arithmetic to putt all the four digits together. – Some programmer dude Nov 12 '21 at 16:56
  • Questions seeking debugging help should generally include a [mre], which includes a function `main` and all necessary `#include` directives. If you hard-code the input, for example with `char buffer[] = "{app_id:9401}";`, and then apply the posted code into a function `main`, are you able to reproduce the error? If yes, then please post this as a [mre]. – Andreas Wenzel Nov 12 '21 at 16:57
  • Since you did not respond to my request for a [mre], I have now converted your posted code to one myself. Feel free to [edit] your question if you are not happy with my changes. – Andreas Wenzel Nov 12 '21 at 18:21

2 Answers2

1

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, "%2d%2d", &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, ':');
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Although this answer provides a good solution to OP's problem, it does not answer OP's question why OP's code is causing a segmentation fault. (I am still upvoting the answer, though.) – Andreas Wenzel Nov 12 '21 at 18:10
0

[...] 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.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39