Thread: Passing pointers to an array

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    22

    Passing pointers to an array

    Hi there;

    I want to pass a pointer to an array to a function, so my second function can modify the array.

    The problem I am coding for is a string - searching for commas and replacing them with blank spaces. I want to abstract this process so I can reuse it in other functions.

    I have done the replace code;

    Code:
    void replacecommas( char line[] )
    {
    
    	unsigned int i;
    
    
    	while ( i < strlen(line) )
    	{
    		if ( line[i] == ',' )
    		{
    			line[i] = ' ';
    		}
    		i++;
    	}
    
    
    
    }
    But have no idea what to pass, how to pass, how to return etc. in terms of arrays (char array - for a string).

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    So you're asking how to call that function?

    Code:
    char data[SIZE];
    /* ... */
    replacecommas(data);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just pass the name of the array (e.g. replacecommas(line)).

    There's also a bug in your code. You're not initializing i to anything. You can only hope that i happens to contain 0 upon creation. You have to explicitly set automatic local variables to 0 for them to be guaranteed to start with that value.

    As an aside, you want to avoid functions like strlen() inside a loop as much as possible. strlen() has to count all of the characters in the string each time it's called. If you create a variable (maybe called something like len) before the loop and set it to the length of the string you can use that variable in the loop without all of the additional CPU load.

    Or you could avoid calculating the length of the string altogether and just check for when line[i] equals 0 ('\0') which marks the end of every string in C. This is actually how strlen() counts the number of characters in a string.

    However, I'd personally probably use something like:
    Code:
    void replacecommas(char *line)
    {
      char *p;
    
      while((p = strchr(line, ',')))
      {
        *p = ' ';
        line = p + 1;
      }
    }
    Last edited by itsme86; 05-10-2006 at 12:27 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    itme's solution is best.

    However I want to point out something else too. Don't call the strlen function in a loop unless you have to. What you are really doing, is looping through a string, so make it obvious.
    Code:
    void replacecommas(char *line) 
    {
       int i = 0;
       for (i; line[i] != '\0'; ++i)
       {
           if(line[i] == ',')
              line[i] = ' ';
       }
    }
    But again, itsme's solution is so much easier.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    22
    Thanks everyone for your help - I went with this solution, and it works

    Code:
    void replacecommas(char *line)
    {
      char *p;
    
      while((p = strchr(line, ',')))
      {
        *p = ' ';
        line = p + 1;
      }
    }

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by citizen
    However I want to point out something else too. Don't call the strlen function in a loop unless you have to. What you are really doing, is looping through a string, so make it obvious.
    I already covered that
    Quote Originally Posted by itsme86
    As an aside, you want to avoid functions like strlen() inside a loop as much as possible. strlen() has to count all of the characters in the string each time it's called. If you create a variable (maybe called something like len) before the loop and set it to the length of the string you can use that variable in the loop without all of the additional CPU load.

    Or you could avoid calculating the length of the string altogether and just check for when line[i] equals 0 ('\0') which marks the end of every string in C. This is actually how strlen() counts the number of characters in a string.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  2. function passing argument..array ?
    By jochen in forum C Programming
    Replies: 2
    Last Post: 09-30-2007, 11:53 AM
  3. Searching an array with pointers
    By pxleyes in forum C Programming
    Replies: 16
    Last Post: 03-23-2004, 05:07 PM
  4. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM