strange scanf behaviour

This is a discussion on strange scanf behaviour within the C Programming forums, part of the General Programming Boards category; Hi all, after alot of java i've decided to start learning some C, i've an exercise that i cannot complete ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    2

    strange scanf behaviour

    Hi all,

    after alot of java i've decided to start learning some C, i've an exercise that i cannot complete because i've a strange (to me) behaviour, here's the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() 
    { 
    	char *strings [10]; 
    	int i, n = 4, str_size = 255;
    	
    	printf ("Enter %d strings to sort:\n", n); 
    	
    	for (i=0; i<n; i++) {
    		strings[i] = (char *) malloc (str_size);
    		scanf ("%s", &strings[i]); 
    	}
    	
    	printf("\nRESULT:\n", &strings[i]); 
    	for (i=0; i<n; i++) {
    		printf("%s \n", &strings[i]); 
    	}
    
    }
    and here is the terminal output

    Code:
    Enter 4 strings to sort:
    one two three four 
    
    RESULT:
    one 
    two 
    threfour 
    four
    What happen to the "three" number? in general all string with a length greater then 3 are concatenated with the following string ...

    i've re-read the scanf documentation but really can't find where's the mistake

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    You dont need & while using %s with scanf and printf. %s takes pointer as its argument. Also no need to cast malloc, it returns void*. Also look at this.
    Code:
    printf("\nRESULT:\n", &strings[i]);
    Last edited by BEN10; 08-10-2009 at 06:27 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose you shouldn't take an address of the char* that strings[i] is.

    Code:
    scanf ("%s", strings[i]);
    printf("%s \n", strings[i]);
    If you happen to use GCC, then turning on warnings can help with errors in format string.

    a.c:13: warning: char format, pointer arg (arg 2)
    a.c:16: warning: too many arguments for format
    a.c:18: warning: char format, pointer arg (arg 2)
    a.c:21: warning: control reaches end of non-void function
    (Actually the warning seems to be incorrectly worded: what you have is char* format and char** argument? Anyway, this shows there is something wrong and it is unlikely to work.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    2
    thanks BEN10 and anon, the error was the use of the "&" operator...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behaviour of GCC
    By BlackOps in forum C Programming
    Replies: 14
    Last Post: 07-29-2009, 06:44 PM
  2. Strange behaviour of scanf()
    By chris1985 in forum C Programming
    Replies: 3
    Last Post: 01-15-2005, 01:41 PM
  3. strange program behaviour...
    By now5 in forum C Programming
    Replies: 10
    Last Post: 03-08-2004, 04:39 AM
  4. GetClientRect strange behaviour
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 10-02-2002, 02:13 PM
  5. Strange behaviour
    By PrivatePanic in forum Windows Programming
    Replies: 11
    Last Post: 07-23-2002, 12:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21