Thread: Pointers and Strings (simple question)

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Pointers and Strings (simple question)

    Hi,

    I am trying copy a string over another string and I wrote the following code:


    Code:
    /* Params: toAssign: The string to overwrite. 
    	   value: The new value of the string  should have
    	   valuelengt: The length of the value*/
    int assignValue(char *toAssign, char *value, int valuelength)
    {
    
    if ((toAssign = malloc(valuelength*sizeof(char)))==NULL) return (0);
    
    int i;
    
    for (i=0;i<valuelength;i++)
    	toAssign[i]=value[i];
    
    toAssign[valuelength] = '\0';
    
    //printf("toassogn: %s",toAssign);
    return(1);
    }
    
    int main()
    {
    
    char string[] = "test1";
    char string2[] = "blablabla";
    
    /*Trying to copy string2 over string  */
    if (!assignValue(string, string2, sizeof string2)){
    	printf("Something went wrong...");
    	return (EXIT_FAILURE);
    }
    
    printf("%s\n", string);
    
    return(EXIT_SUCCESS);
    }
    In this case I am trying to copy string2 ("blablabla") over string ("test1")

    If I get it correctly when calling assignValue the pointers of the first elements of "string" and "string2" are passed into the function. In the function I allocate new memmory space and I make the pointer toAssign to point to that space. Then I copy the elements of the second array to the newly allocated space.

    When assignValue exits and the control is returned to main "string" should point to the newly allocated memory, right? Well, it doesn't (i.e. the printf) statement in main() prints "test1"). So I guess something is wrong in the way I think about it?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    Hmmm.. I think I know what is wrong. The pointer cannot change because it is passed as a parameter to the function (in the same way a value cannot change if it is passed as a parameter to a function).

    Maybe if I pass as a parameter a pointer to the pointer and manipulate that in the assignValue? I'll give it a shot now.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You don't need to pass valueLength, because the string is (shall ... ) be ended with a NULL character.

    Is this a homework or something that you need to provide your own string copy function?
    If not, use strcpy/strncpy.

    And everything you allocate you must FREE.

    Your code could even work if you pass a pointer to pointer (but don't do this).
    Last edited by kmdv; 09-23-2010 at 06:43 AM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    3
    Thanks for the reply kmdv. I am just preparing for a project in C and I am playing around with various C concepts (haven't used C since my bachelor's 12 years ago!).

    Quote Originally Posted by kmdv View Post
    You don't need to pass valueLength, because the string is (shall ... ) be ended with a NULL character.
    That's a very good point, thank you.

    Quote Originally Posted by kmdv View Post
    And everything you allocate you must FREE.
    Thanks for reminding....it shows I have a strong C# background!

    I found out that I am not going to go very far with this approach.The best way to do it is like strcpy...(i.e. avoid allocating memory in the function). Instead ask the caller to allocate the amount of memory required and my function to just do the copying.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you do that, require that the caller also specify the size of the buffer they pass in. Use it to avoid buffer overruns.
    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. newbie question on strings & pointers
    By MK27 in forum C Programming
    Replies: 6
    Last Post: 08-05-2008, 05:50 AM
  2. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question about pointers
    By maxhavoc in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2004, 12:46 AM
  5. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 09:13 AM