Thread: NOOB Question!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    167

    NOOB Question!

    We have a function like this:

    Code:
    void DS_append(DynString ds, char *newtxt)
    And we need to ask the user for an integer input and then append it. So I have:

    Code:
      int x;
      printf("Please enter a number: ");
      scanf("%d", &x);
    
      DS_append(ds, x); // This is line 29
    But when I compile it gives me:
    Code:
    ex5.c:29: warning: passing argument 2 of ‘DS_append’ makes pointer from integer without a cast
    And when I run it, of course, the much dreaded...
    Code:
    Segmentation fault
    I've tried every combination of
    DS_append(ds, &x);
    DS_append(ds, (char) x);
    DS_append(ds, (char *) x);
    DS_append(ds, (char *) &x);

    But none seem to work.

    Can anyone figure out what I'm doing wrong here?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need a string, you have an integer. You need to convert from one to the other (I would recommend sprintf to "print" the integer into a string).

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by tabstop View Post
    You need a string, you have an integer. You need to convert from one to the other (I would recommend sprintf to "print" the integer into a string).
    After some quick googling:

    Code:
      int x;
    
      printf("Please enter a number: ");
      scanf("%d", &x);
    
      char result[100];
      sprintf( result, "%d", x );
    
      DS_append(ds, result);
    Works great! THANK YOU!!!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Paul22000 View Post
    After some quick googling:

    Code:
      int x;
    
      printf("Please enter a number: ");
      scanf("%d", &x);
    
      char result[100];
      sprintf( result, "%d", x );
    
      DS_append(ds, result);
    Works great! THANK YOU!!!
    What is the purpose of taking the user's input, which is already a string, converting it to an integer with scanf(), only to convert it directly back to a string again with sprintf()? Why not just scan the user's input using '%s' instead of '%d' ?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    167
    Quote Originally Posted by brewbuck View Post
    What is the purpose of taking the user's input, which is already a string, converting it to an integer with scanf(), only to convert it directly back to a string again with sprintf()? Why not just scan the user's input using '%s' instead of '%d' ?
    We have to do some stuff with the int that is inputted. I thought about just doing that too hehe, but yeah, there is a reason for it being an int in the first place

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick noob question
    By thanatos1 in forum C# Programming
    Replies: 2
    Last Post: 06-17-2009, 08:28 PM
  2. another noob question
    By clb2003 in forum C Programming
    Replies: 4
    Last Post: 02-12-2009, 01:28 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. Very noob question :(.
    By GamerProduction in forum Tech Board
    Replies: 4
    Last Post: 04-14-2007, 05:40 AM
  5. Noob question ( little dos program )
    By Demon1s in forum C++ Programming
    Replies: 13
    Last Post: 04-04-2003, 09:28 PM