passing character to a function

This is a discussion on passing character to a function within the C Programming forums, part of the General Programming Boards category; What I basically have is: Code: int main() { char * myText; myText = "blah"; function(myText); return 0; } function(char ...

  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
    Katy, Texas
    Posts
    2,309
    Do this:

    function(&myText[1]);
    Last edited by Dino; 12-02-2008 at 10:29 AM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    32,855
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    22,312
    What is your current code?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    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
    Posts
    23,033
    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, 07:06 AM
  2. Passing A Function From One Object to Another
    By HalNineThousand in forum C++ Programming
    Replies: 8
    Last Post: 03-28-2008, 08:26 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04: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

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