Thread: Passing char array to fuction

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    Question Passing char array to fuction

    I have this:
    Code:
    #include "func.h"
    
    int main()
    {
    	char array[5] = "hello";
    	func(array);
    }
    in another file I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <sys/types.h>
    #include <dirent.h>
    
    #include "pgmfile.h"
    #include "malloc_image.h"
    
    void func(char array[])
    {
    	printf("%s\n", array);
    }
    but when i run it it prints hello along with other characters. why. what can i do?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by taurus View Post
    I have this:
    Code:
    #include "func.h"
    
    int main()
    {
    	char array[5] = "hello";
    	func(array);
    }
    but when i run it it prints hello along with other characters. why. what can i do?
    Your array has five elements, but you actually need six (one for the terminating nul character '\0').

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by taurus View Post
    I have this:
    Code:
    #include "func.h"
    
    int main()
    {
    	char array[5] = "hello";
    	func(array);
    }
    Initializing more elements in the array than its size is an error except for char where it's not. The '\0' terminator will not get stored and thus after printing hello it doesn't know where to stop and prints unwanted chars after that too.
    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

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    49
    In the function store the char array in a string and print the string?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by kairozamorro View Post
    In the function store the char array in a string and print the string?
    A char array is a string...
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    49
    Yes, but to avoid the extra characters, etc.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub View Post
    A char array is a string...
    No if it doesn't have a null terminator it's not.


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

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by quzah View Post
    No if it doesn't have a null terminator it's not.


    Quzah.
    How enlightening...
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub View Post
    How enlightening...
    Well considering the array in the OP isn't a string, yes, yes it was.


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

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by quzah View Post
    Well considering the array in the OP isn't a string, yes, yes it was.


    Quzah.
    Cool, then explain to me how you can null terminate that from within the function:
    Code:
    void func(char array[])
    {
    	/* convert this char array to a string before the print */
    	printf("%s\n", array);
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't. I know you're trying to be clever here, but you aren't. At all. You have to know the answer to this. You cannot know the size of an array once it is passed to a function. You cannot guarantee that array is a string.

    You aren't passing the size, so you can only assume. You might be right, you might not.
    Code:
    char oops[1] = {'x'};
    ...
    func( oops );
    Perfectly legal.


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

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by quzah View Post
    You don't. I know you're trying to be clever here, but you aren't. At all. You have to know the answer to this. You cannot know the size of an array once it is passed to a function. You cannot guarantee that array is a string.

    You aren't passing the size, so you can only assume. You might be right, you might not.
    Code:
    char oops[1] = {'x'};
    ...
    func( oops );
    Perfectly legal.


    Quzah.
    Exactly. Now look at my first post in the context of what I quoted. My point is that you can't convert a char array to a string in the function because to the function there is no difference. The function must assume that the passed array is a string.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bithub View Post
    Exactly. Now look at my first post in the context of what I quoted. My point is that you can't convert a char array to a string in the function because to the function there is no difference. The function must assume that the passed array is a string.
    Well that's not exactly true. If you know the size of an array, which you could do by including it as an argument, you can make it a string any time you want by adding a terminating null anywhere < size.

    But pretty much we were both trying to get the same point across to them, so I see what you were saying.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  2. linked list inside array of structs- Syntax question
    By rasmith1955 in forum C Programming
    Replies: 14
    Last Post: 02-28-2005, 05:16 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. passing char where array of chars required
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 01:24 PM