strcpy and strdup?

This is a discussion on strcpy and strdup? within the C Programming forums, part of the General Programming Boards category; *buf[0]={"Tension"}; How do I use 'strcpy' or 'strdup' in this situation below: buf[0][0]='T' copy into buf1[0][0]; buf[0][1]='e' copy into buf1[0][1]; ...

  1. #1
    Unregistered
    Guest

    Question strcpy and strdup?

    *buf[0]={"Tension"};

    How do I use 'strcpy' or 'strdup' in this situation below:

    buf[0][0]='T' copy into buf1[0][0];
    buf[0][1]='e' copy into buf1[0][1];
    buf[0][2]='n' copy into buf1[0][2];

    buf[0][3]='s' copy into buf1[1][0];
    buf[0][4]='i' copy into buf1[1][1];
    buf[0][5]='o' copy into buf1[1][2];

    buf[0][6]='n' copy into buf1[2][0];


    In my code I tried like this in loops but it generates some warning:

    strcpy(buf1[a][b], buf[c][b]);

    warning C4024: 'strcpy' : different types for formal and
    actual parameter 1.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I think you only need to use the first bracket in the strcpy as the second refers to a individual character in the string and strcpy copies strings not characters

    strcpy(buf1[a], buf[c]);

    I havent tried this as i use pointers when dealing with strings(much easier) so I could be wrong, but give it a go
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Unregistered
    Guest

    Unhappy same problem.

    The same problem exists even I write like this:

    buf1[a][b] = buf[c][b]

  4. #4
    Unregistered
    Guest

    source code

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    unsigned int a, b, c, pc, d;
    char pbuf[10][5];

    char *array[3]={ "smelly thingy",
    "Hello world man",
    "piece of cake hellol asdjfksd asdfsf adsfa sfaafa"};

    for(a=0; a<50; a+=5)
    {
    for(b=0, pc=1; b<3, pc<50; b++, pc++)
    {
    for(c=a, d=0; c<(a+5), d<5; c++, d++)
    {
    if ( c < strlen(array[b]) )
    {
    pbuf[pc][d] = array[b][c];
    printf("%c", pbuf[pc][d]);
    }

    }
    printf( "\n" );
    }
    printf( "-----\n" );
    }
    return 0;
    }

    The buf and buf1 are just an example but in this code they refer to 'pbuf' and 'array'.

    The reason why I put 5 bytes per 5 bytes into independent array is to make protocol. After splitting into 5 bytes and storing into independent array i.e. pbuf[pc][d], I will use strcat or strncat to concatenate with a header just like in protocolling but the compiler generates some error.
    The way that I declare the pbuf[10][5] again is not memory friendly.


    Its there any better way to do this. Suggestions are welcome.
    Lastly, thank you for your consideration, Salem. Same goes to C_Coder, thank you for your explanation.

  5. #5
    Unregistered
    Guest
    Ug. That just looks nasty. Here's a thought:
    Code:
    for(b=0, pc=1; b<3, pc<50; b++, pc++) 
    { 
       for(c=a, d=0; c<(a+5), d<5; c++, d++) 
       { 
          if ( c < strlen(array[b]) ) 
          { 
             pbuf[pc][d] = array[b][c]; 
             printf("%c", pbuf[pc][d]);
    Can anyone here spot the obvious flaw with this logic?

    Ah, here it is:

    pbuf[ up to 50! ][ upto 5 ]

    Ok, so where have you declared a pbuf array that is potentially 50x5? You haven't.

    Quzah.

  6. #6
    Unregistered
    Guest

    typo error!

    The variable pc = a not pc=1! sorry!

  7. #7
    Unregistered
    Guest
    Dear Quzah,
    The problem still exists, eventhough I declared pbuf[50][5].

  8. #8
    Unregistered
    Guest

    Thumbs up Brilliant!

    Dear Salem,

    Thank you for your brilliant source code but this code unable for me to do the time division multiplexing if compared to my previous code. Anyway, I will try to do the time division multiplexing by expanding your code. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy, strdup
    By shibu in forum C Programming
    Replies: 6
    Last Post: 09-13-2006, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21