0

I have used sscanf to scan in a string and add it to a structure. The only problem is that I cannot print out the string because there is no null terminator added by default. I tried to add a null in there with the strcat() function but came to the realization this cannot work. Thanks for any help.

struct a
{
int *ref;
char string[50];
}rec;

void task()
{


   char *p_test;

   char test[50] = "9999:STRING OF TEXT";
   p_test = test;

   sscanf(p_test, "%d:%[^\n]", &rec.ref, rec.string);

   printf("String is:%s", &rec.string);

}
3
  • 1
    Do correct the syntax errors buddy. Commented Dec 12, 2014 at 19:26
  • Thanks for the help everyone, I am still getting the syntax errors: incompatible implicit declaration of built-in function 'sscanf' [enabled by default]| and incompatible implicit declaration of built-in function 'printf' [enabled by default] and im not sure why. How am I using these functions wrong? Commented Dec 12, 2014 at 19:51
  • You must include the header files that define the library functions you use ... in this case you need #include <stdio.h> Commented Dec 12, 2014 at 20:27

2 Answers 2

3

There are multiple problems with your code.

  1. test[50] = "9999:STRING OF TEXT";
    

This is wrong for two reasons.

A) test is an array of char, not an array of char*. So, when you assign a string (a char*) to an element, the address is converted to a char.

B) Element 50 does not exist in your array, and writing to it invokes undefined behavior. You have an array of 50 elements with indices 0...49.

To assign an initial value to your array, all you need do is:

char test[50] = "9999:STRING OF TEXT";

And since test does not need to be modified...

const char *test = "9999:STRING OF TEXT";
  1. If you want to zero an array, the simplest method is:

    char test[50] = {0};
    

Of course, you don't need to if you assign the string properly to begin with, and this is not your problem.

  1. Your string member of the a struct is a char, not a char* (a string), and using the %s format specifier when printing it invokes undefined behavior.

  2. main is defined to return int, not void.

  3. sscanf expects pointers to data to fill in. rec.ref is an int, you need to pass its address, i.e., &rec.ref.

  4. You need to allocate storage for rec.string.


Here is a working example:

#include <stdio.h>

#define STRING_LEN 50

struct a
{
    int ref;
    char string[STRING_LEN];
} rec;

int main()
{
    char test[STRING_LEN] = "9999:STRING OF TEXT";

    // note that, in the real world, this may 
    // be a buffer overflow waiting to happen
    sscanf(test, "%d:%[^\n]", &rec.ref, rec.string);

    printf("String is:%s, rec is:%d", rec.string, rec.ref);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have a number of problems; a lack of null-terminator actually isn't one of them.

One problem is a syntax error; this:

   char test[50]
   test[50] = "9999:STRING OF TEXT";

is not valid syntax. You need:

   char test[50] = "9999:STRING OF TEXT";

Another problem is that rec doesn't have a string (a char * or char[]), it just has a single character (a char). A simple approach, to get you started, is:

struct a
{
    int ref;
    char string[50];
} rec;

A third problem is that sscanf's arguments all have to be pointers. C is a pass-by-value language; if you just pass in an integer, that doesn't give sscanf any way to modify that integer:

sscanf(test, "%d:%[\n]", &rec.ref, rec.string); // (once rec.string is a pointer)

A fourth problem is that the format-specifier %[\n] does not mean what you seem to want it to mean. (Maybe you actually wanted %[^\n]?)

Other, smaller issues include your return-type for main (it should be int, not void).

The common thread with most of these issues is that your compiler would have eagerly helped you identify them, had you simply turned on compiler warnings!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.