Thread: pointers

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    13

    pointers

    hi,
    I need to build a function with a pointer .

    void split(char *src, char c);

    the function gets a string and a char and doing this:

    Code:
    char *src=“Big-Brother”, c='-';
    split(src,c);
    
    output:
    B-i-g---B-r-o-t-h-e-r
    can u give me ideas how to build it.?!

    thank u !

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Get rid of those non-C type double quotations around Big Brother, because C won't recognize them.

    Use the key on your middle row, right hand side (QWERTY type keyboard).

    Other than that, you need to sit down and try working at it. You've got enough info to get started, so get started.

    If you get stuck, THEN post back. We're here to solve (or try to), problems with C programming, not problems with being lazy.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    13
    I'm not lazy I tried to that but I have a logical problem that I dont know how to solve
    that what I did:

    Code:
    void split(char *src, char c)
    
    {
    	char string[LENTH];
    	char temp=0;
    	
    	src = &string[0];
    	
    	
    	while (*src != '\0')
        {
    		src++;
    		temp=*src;
    		*src= c;
    		src++;
    		*src=temp;  
    
        } 
      
    }

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    10
    do you want to return a string or print it out?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    From the name of the function, and your description and code, I'm not sure what you're trying to do in split().

    I thought you wanted to "walk" through the string, and print it out, with a hyphen between each letter or space.

    You shouldn't be trying to change *src, because it points to a string literal, and you can't change them (well, you shouldn't, even if your compiler allows it).

    So what is split() trying to do?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You should always assign string literals to const char * instead of char *, because they cannot be modified. The ability to do otherwise is for backwards compatibility only.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM