Thread: string copy function

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    58

    string copy function

    Code:
    # include <stdio.h>
    # include <String.h>
    int main(void){
        int i=0; int j=0;
        
        char *pairA[3];
        char  pairB[10];
        
        pairB[0]='D';
        pairB[1]='o';
        pairB[2]='g';
        pairB[3]='\0';  
    
        strcpy(pairA[0],pairB);
    }
    How would I copy the pairB onto pairA ? The program compiles,but does run .

    Any other better way to assign the word dog to to pair B ?
    would be grateful if some-one can help me out.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        char *pairA[3];
        char  pairB[10] = "Dog";
    
        pairA[0] = malloc(strlen(pairB)+1);
        strcpy(pairA[0], pairB);
        free(pairA[0]);
    }
    [edit]
    Dynamic memory might not be something a beginner should start out with, an easier way would be to make pairA a 2D array instead of a an array of char pointers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void){
        char pairA[3][10];
        char  pairB[10] = "Dog";
    
        strcpy(pairA[0], pairB);
    }
    Last edited by OnionKnight; 02-13-2006 at 08:22 PM.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by rahulsk1947
    [
    Any other better way to assign the word dog to to pair B ?
    Well, you could simply make pairA a pointer to your string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char  *pairA;
        char  pairB[10];
    
        pairB[0]='D';
        pairB[1]='o';
        pairB[2]='g';
        pairB[3]='\0';  
    
        pairA = pairB;
        
        printf("%s", pairA);
    }
    Ofcourse if pairB changes... so does pairA.
    Last edited by SlyMaelstrom; 02-13-2006 at 08:27 PM.
    Sent from my iPad®

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Technically, 'pairA' doesn't change. The value it contains is still the address of 'pairB'. It appears to change if you dereference it, because the value of what it points to has changed.


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

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rahulsk1947
    How would I copy the pairB onto pairA ? The program compiles,but does run .

    Any other better way to assign the word dog to to pair B ?
    would be grateful if some-one can help me out.
    I'd go with something like this.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char pairB[] = "Dog", pairA[sizeof pairB];
       strcpy(pairA, pairB);
       /* do stuff */
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    Hmm,so
    Code:
    char pairB[10]="dog";
    is the way to assign it.

    infact I been trying to do
    Code:
    char pairB[10];  pairB="dog";
    but i get compilation errors.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    58
    actually i am trying to do

    Code:
    #include <stdio.h>
    
    struct student{
        char name[25];
        int age;
        int idNum;
    };
    
    int main(void){
        struct student a;
        
        a.age=21;
        a.idNum=77273869;
        // a.name="rahul"; -> gives an error
        
    }
    how to assign my name to a.name ??

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    memcpy(a.name,"rahul",strlen(a.name)+1);  // include string.h>
    
    or
    
    strcpy(a.name,"rahul");  // < string.h >

    ssharish2005

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't make assignments to entire arrays except at declaration time. You must do a cell at a time. You can use the standard library function strcpy to do this for you. All it does is loop through one string, copying it to another. Dave has already showed you how to use it up above.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM