Thread: Assign array to another.

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30

    Assign array to another.

    Hi, i am trying to get one char array to be assigned to another.
    I have a struct called root which has a field called num, this is a char array of length 12.
    One function i have tries to double the length of the char array by creating a new one double the size and copies everything in the first array into the new one, as well as some more chars. At the end i try:

    Code:
    root->num = newNum ;
    but this won't compile, i get incompatible types in assignment!
    any help would be great, i'm quite new to C so be nice :P
    -ed

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can't assign to arrays like that, you can copy one to another using strcpy() otherwise you'll get the "incompatible types" compile time error.
    OTOH you can assign to structs. If both arrays, num and newNum, are the only members of struct root then you can assign one struct to another.
    The net effect of the struct assignment will be that array newNum will get copied into array num.

  3. #3
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    the struct has more fields, which cancels out that idea i assume. How would i go about using strcpy()? And will this make the num field have 24 characters instead of 12 or will it just loop round and start at the beginning of the array after 12 characters?

    EDIT:

    tried:
    Code:
    strcpy(root->num , newNum)
    but the array is still only 12 chars, but its content hasn't changed either...weird!
    Last edited by boblettoj99; 12-03-2009 at 03:50 PM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by boblettoj99 View Post
    the struct has more fields, which cancels out that idea i assume. How would i go about using strcpy()? And will this make the num field have 24 characters instead of 12 or will it just loop round and start at the beginning of the array after 12 characters?
    Neither, what will end up happening is a segfault because you have stepped out of bounds of array num[]. You can't stuff more than what you have allocated for num[].
    Quote Originally Posted by boblettoj99 View Post
    EDIT:

    tried:
    Code:
    strcpy(root->num , newNum)
    but the array is still only 12 chars, but its content hasn't changed either...weird!
    Aren't the first 12 elements of num[] and newNum[] identical?

  5. #5
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    Yeh, sorry, it's doing what it should. Is there any way i can make this work then? Other than making num have a ridiculous amount of space in the struct definition?

    EDIT: (assuming i run this function an unknown number of times)

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Short answer, you can't.

    to do what you want, you can

    memcpy((void*)root->num, (void*)newNum, sizeof(newNum));

    which implies that newNum is an array and not a pointer. If newNum is a pointer you need a size_t len of newNum.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Location
    UK
    Posts
    30
    argh, ok, well i'll have to find some new way of getting round this problem then, thanks for the help!

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by boblettoj99 View Post
    Yeh, sorry, it's doing what it should. Is there any way i can make this work then? Other than making num have a ridiculous amount of space in the struct definition?

    EDIT: (assuming i run this function an unknown number of times)
    Not sure what you mean by the above statement

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to dynamically expand the storage in some field, then you will have to use dynamic memory: malloc and free along with a pointer, of course.
    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. Assign null to a character array.
    By mahedee in forum C Programming
    Replies: 1
    Last Post: 08-06-2008, 07:16 AM
  2. Replies: 6
    Last Post: 02-27-2006, 03:11 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Assign value of multiple char array elements to int
    By 3saul in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 05:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM