Help - rewriting style of string library in C

This is a discussion on Help - rewriting style of string library in C within the C Programming forums, part of the General Programming Boards category; Originally Posted by quzah Pretty much just what it says. It wants you to pass a character pointer, and instead, ...

  1. #16
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    Quote Originally Posted by quzah View Post
    Pretty much just what it says. It wants you to pass a character pointer, and instead, you're passing an integer.
    Code:
    int x = 0;
    printf( "%s", x );
    Quzah.
    but im passing it an array that contains chars...
    Code:
    int i;
    for (i = 1; i < dest[0]; i++) {   //dest[0] has the length of the array in this spot
      printf("%s", dest[i]);              //error is here
    }

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Is dest a char pointer? If so then dest[i] is a single character and needs %c not %s. Characters are basically integers with a more limited range which is why you get the "argument 2 has type 'int'" part of the error message.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    i have got it almost working, my output is just a bit weird, im gonna stop posting my code in case other students in my class start using my code as well, most of the code from my previous posts has changed a lot in order to get it working.

    i wish there was a way for me to show you my code because im getting such a weird output, i know it must be a math error somewhere but i just can't find it, thanks for all the help everyone i have learned a lot today

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your output errors are probably because you're trying to print your values as strings, when they aren't strings. Your fake strings aren't null terminated. That means you can't use any normal string functions on them.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    well my output is
    Code:
    1 0 -1
    helloworlo
    supposed to be
    Code:
    1 0 -1
    hello world
    im not understanding where i went wrong

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You didn't add 1. You're adding to the end of the string, which means you're starting at 7, not 6.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    i think my ps_set is wrong because i just added a printf statement to tell me the length

    and "hello " = 6
    and "world\n" = 6

    but world is only supposed to be 5 :S

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is it only supposed to be five? "world\n" has six characters.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered User
    Join Date
    Nov 2009
    Posts
    16
    because \n is an escape key

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    w = 1
    o = 2
    r = 3
    l = 4
    d = 5
    \n = 6

    Where are you confused? Unless your set function is designed to treat \n as the end of a string, which it shouldn't, because otherwise 'hello ' should have kept on checking for more letters, then \n is just another regular character.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Replies: 12
    Last Post: 06-08-2005, 11:23 AM

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