Thread: sscanf error

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    sscanf error

    Just trying to see what the sscanf does and got stuck with the following program. Though i can understand the error (trying to write to an address which is random , this is what i think). let me know if i am wrong

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char * ints = "20, 40, 60";
    	char * floats = "10.5,  12.5";
    	char * hexs = "FF, F";
    
    	int i;
    	int n;
    	float f;
    	int h;
    	char *s;
    
    	n = sscanf(ints , "%d", &i);
    	printf("\n %d", i);
    	printf("\n sscanf returns %d\n\n", n);
    
    	n = sscanf(floats , "%f", &f);
    	printf("\n %f", f);
    	printf("\n sscanf returns %d\n\n", n);
    
    	n = sscanf(hexs , "%x", &h);
    	printf("\n %d", h);
    	printf("\n sscanf returns %d\n\n", n);
    
    	n = sscanf(ints , "%s", s); // ERROR IS HERE 
    	printf("\n %s", s);
    	printf("\n sscanf returns %d\n\n", n);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are correct; you are trying to write to unallocated memory. s is just a pointer, there is no room to write a string there.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But how do i overcome it. When i try to assign some address like

    s = floats;

    still the error persists. Now s is assigned some valid address and i can copy the string from ints to s. But still its the same problem.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    char s[64];
    // or
    char* s = malloc(64);
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks , Yes it works now using malloc but what is wrong with the assignment

    s = floats

    If i am not wrong floats is now a string literal and its illegal to modify a string literal in C.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If i am not wrong floats is now a string literal and its illegal to modify a string literal in C.
    You cannot modify string literals. String literals are stored in read-only memory. In fact, you should declare all pointers to string literals as const so you don't accidentally make this mistake.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks i get it now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM