Thread: passing character to a function

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    passing character to a function

    What I basically have is:

    Code:
    int main()
    {
       char * myText;
       myText = "blah";
       function(myText);
       
      return 0;
    }
    
    
    function(char * text)
    {
       //do stuff
    }
    what I want to be able to do is pass a single character to my function, however when do this:

    Code:
    function(myText[1]);
    It doesn't work and I get compiler errors.

    What I want is the way to pass a single character to this function? Note that I still want to be able to pass whole arrays to this function.

    Any help would be appreciated

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Do this:

    function(&myText[1]);
    Last edited by Dino; 12-02-2008 at 10:29 AM.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Single character = char.
    Array of characters = char*.
    Just remember that you cannot modify a string literal, thus the code:

    Code:
       char * myText;
       myText = "blah";
    ...should be...
    Code:
       const char * myText = "blah";
    That will prevent mistakes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, you can pass a single character to a function. However, your function prototype requires a pointer to character, and as you are probably aware, a pointer to a character, and a character are not the same thing - just like the address where I live is not the same as my person.

    Do you want to pass a pointer, or just a single character?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    function(&myText[1]);
    would work, but it doesn't help you figure out whether you meant 1 character or a string.

    Both are the same as far as function is concerned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Quote Originally Posted by matsp View Post
    Of course, you can pass a single character to a function. However, your function prototype requires a pointer to character, and as you are probably aware, a pointer to a character, and a character are not the same thing - just like the address where I live is not the same as my person.

    Do you want to pass a pointer, or just a single character?

    --
    Mats
    Sorry, I want to pass the pointer

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So like Salem or Dino says, &myText[x] will pass the address of character x.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    If I use &myText[1] It takes the address of the character, but all subsequent characters are also included..

    so in this example, mytext = lah instead of l

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your current code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by RedWind View Post
    If I use &myText[1] It takes the address of the character, but all subsequent characters are also included..

    so in this example, mytext = lah instead of l
    You fail to understand.
    It takes the address to the character ONLY.
    However, as it is a pointer (and a pointer is a variable), it can be incremented to the next character in memory.
    In C, all strings are passed as char* and the memory is traversed byte by byte until the '\0' character is found. This gives the "illusion" that when you pass the char*, you get all subsequent characters, as well, but this is not correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    16
    Quote Originally Posted by Elysia View Post
    You fail to understand.
    It takes the address to the character ONLY.
    However, as it is a pointer (and a pointer is a variable), it can be incremented to the next character in memory.
    In C, all strings are passed as char* and the memory is traversed byte by byte until the '\0' character is found. This gives the "illusion" that when you pass the char*, you get all subsequent characters, as well, but this is not correct.
    ah of course, that makes sense. so all I have to do is set the next byte to '\0' and I've solved my 'problem'.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed! And thus you've created a string with only 1 character!
    That sounds silly, but it is a solution. Good job figuring it out, though. I commend you for that
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing A Function Into A Constructor
    By fourplay in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2009, 06:06 AM
  2. Passing A Function From One Object to Another
    By HalNineThousand in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2008, 07:26 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  5. Passing a function to a function
    By lend0g in forum C++ Programming
    Replies: 1
    Last Post: 03-18-2003, 10:16 PM