Problem

This is a discussion on Problem within the C Programming forums, part of the General Programming Boards category; I just ran into to a problem while working on the project. The thing is when I'm working on replacing ...

  1. #1
    Unregistered
    Guest

    Angry Problem

    I just ran into to a problem while working on the project. The thing is when I'm working on replacing a string, I want to use multidimentional arrays as
    mystings[5][40];
    but I really donno how to apply them in replacing the string; however, I know how to use 2 strings as

    void replace_strings(char*s1,char *s2){
    char *temp;
    temp=(char*)malloc(strlen(s)+1, size of char);
    if(!temp){M.A.F.}
    if (strcmp(s1,s2)>0){
    strcpy(temp,s1);
    strcpy(s1,s2);
    strcpy(s2,temp);
    free(temp);
    }
    return;
    }


    Any kind of help is greatly appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use strncpy.

    strncpy( myarray[0 through 4], fromhere, 39 );

    Now then, you want to use 39 here, so you can null terminate it manually.

    ie:

    strncpy( myarray[0], myarray[1], 39 );
    myarray[39] = NULL:

    Quzah.
    Last edited by quzah; 04-01-2002 at 11:22 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest

    Unhappy

    Thanx for the reply Quzah,
    I 'm not really good at programming. I went through the link u sent. Let me write what I understand from your explanation;

    char mystrings[5][40];
    char temp;

    strncpy(mystrings[0],temp,39);
    strncpy(mystrings[0+1],temp,39);
    strncpy(mystrings[0+2],temp,39);
    strncpy(mystrings[0+3],temp,39);
    strncpy(mystrings[0+4],temp,39);

    Is it right

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Two things:

    strncpy(mystrings[0+1],temp,39);
    strncpy(mystrings[0+2],temp,39);
    strncpy(mystrings[0+3],temp,39);
    strncpy(mystrings[0+4],temp,39);

    You can just use:

    strncpy( mystrings[1], ... )

    No need in using "0+3" or whatever.

    The second thing is that:

    char temp;

    Here, 'temp' is not an array. It's a single character. Replace it with something like:

    char temp[40];

    Then, for example, if we were swapping places 1 and 2 (not 0 and 1):

    strncpy( temp, mystrings[1], 39 );
    strncpy( mystrings[1], mystrings[2], 39 );
    strncpy( mystrings[2], temp, 39 );


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

  5. #5
    Unregistered
    Guest
    Thak you so much Quzah, I really apprecaite it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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