Thread: sscanf using '%p' fails to process.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Mt Umunum, CA

    sscanf using '%p' fails to process.

    Hi group,
    I am fighting with sscanf again! I am attempting to split a string using the '%p' format. Have not been able to find the correct syntax with many searches. My test program:
    #define _GNU_SOURCE
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <termio.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    int    RC, L, Size, N;
    char   Buffer[1024];
    char   F, C;
    int main(  int argc, char* argv[] )  {
      Buffer[0] = '\0';
      memcpy( Buffer+1, Buffer, 1023 );
      unsigned  long  X;
      unsigned  long *X2X = &X;
      sprintf( Buffer, "-10%sZ123456789XYZ", ":" );
      printf( "X: %x:%x, X2X: %x:%x\n", X, &X, X2X, &X2X);
      RC = sscanf( Buffer, "%c%d%*c%p", &F, &L, &X2X );
      printf( "RC    : %d \n ",   RC     ); 
      printf( "X: %x:%x, X2X: %x:%x\n", X, &X, X2X, &X2X);
      return 0;  }
    And I get this:
    Starting program: /src/RHID/src/rhid/a.out 
    X: 0:ffffdf08, X2X: ffffdf08:ffffdf00
     RC    : 2 
    X: 0:ffffdf08, X2X: ffffdf08:ffffdf00
    The return code of 2 is vaild since only 2 variables are processed.

    Can anyone tell me what the correct format is to return a pointer using '%p'?

    Thanks for your time.

  2. #2
    Registered User
    Join Date
    Jun 2015
    The purpose of your code is incomprehensible to me.

    You need to use a higher warning level on your compiler. You are using long unsigned ints but are using the format spec for regular unsigned ints. And you should be printing the addresses with the %p format spec (and casting them to void*).

    It is undefined behaviour to use memcpy to copy data between overlapping objects. Your memcpy is pointless anyway since Buffer is automatically zeroed by being defined globally. To properly explicitly zero it you should use memset, not memcpy (and not even memmove, which does allow copying between overlapping objects).

    The correct format for %p with the scanf-type functions is implementation-defined but is the same as that printed with %p by the printf-type functions. I've used the format for my impl. below (simply 0x followed by the hex).
    #include <stdio.h>
    int L;
    char Buffer[1024];
    char F, C;
    int main(void)  {
      unsigned  long  X;
      unsigned  long *X2X = &X;
      sprintf(Buffer, "Z10:0x777777777777");
      printf("X: %lx:%p, X2X: %p:%p\n", X, (void*)&X, (void*)X2X, (void*)&X2X);
      sscanf(Buffer, "%c%d%*c%p", &F, &L, (void**)&X2X );
      printf("X: %lx:%p, X2X: %p:%p\n", X, (void*)&X, (void*)X2X, (void*)&X2X);
      return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux process trees and process chains
    By elururajesh in forum Linux Programming
    Replies: 2
    Last Post: 07-20-2015, 06:22 AM
  2. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  3. Why this fails?
    By manav in forum C++ Programming
    Replies: 13
    Last Post: 05-29-2008, 01:11 PM
  4. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM

Tags for this Thread