2
int y = strlen(target);
sscanf(p,"%*s",y,buffer);

Why does the above code result in warning: data argument not used by format string ??

Compiled with Apple clang version 11.0.0 (clang-1100.0.33.17).

Aim : To get the same number of characters as in a string target into the string buffer, where p is a char * pointing to some element of a string.

user13863346
  • 327
  • 2
  • 11

3 Answers3

4

Other answers explain that sscanf does not provide for taking field widths from arguments. But with respect to this:

Aim : To get the same number of characters as in a string target into the string buffer, where p is a char * pointing to some element of a string.

I/O functions such as sscanf() are comparatively heavyweight. If all you want to do is copy (part of) one string to another, then memcpy() or strncpy() would be more suited to the task. Or strncat(), whose properties make it superior to the other two for this particular task, as with it you do not have the risk of overrunning your source string that memcpy() would bring, or the need to manually ensure termination of the result that comes with both memcpy() and strncpy(). Example:

*buffer = '\0';         // start with an empty string
strncat(buffer, p, y);  // concatenate the wanted region of the source string
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
3

With sscanf(), the * in "%*s" is an argument suppression, not a size.

sscanf(p,"%*s",y,buffer); simply scans p for a non-white-space string without saving anything. y, buffer are unused.


Recommend to research fgets().

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
2

I believe you want

#include <string.h>

memcpy(buffer, p, strlen(target));

Note that this will not add a terminating NUL. This can be achieved as follows:

#include <string.h>

size_t len = strlen(target);
memcpy(buffer, p, len);
buffer[len] = 0;
ikegami
  • 367,544
  • 15
  • 269
  • 518