A "wrong type" warning

This is a discussion on A "wrong type" warning within the C Programming forums, part of the General Programming Boards category; There's no reason to copy strings, he could just swap the pointer values... Code: temp = str1; str1 = str2; ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There's no reason to copy strings, he could just swap the pointer values...

    Code:
    temp = str1;
    str1 = str2;
    str2 = temp;

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    There are several problems with that.
    First, those changes will be local to the function. That's not the point of the whole.
    Secondly, zel wants to truncate everything in the string after N characters. Your code does not take that into account, though it is possible.
    Thirdly, only N characters from the source should be copied to dst. The source shall remain unaffected!
    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.

  3. #18
    zel
    zel is offline
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    So I did this:

    Code:
    void copy( char* str1,char* str2, int N) {
    
     int i,len1,len2;
     
      for (i=0; str1[i] != '\0'; i++){
        len1= i;
       }
       for (i=0; str2[i] != '\0'; i++){
        len2= i;
       }
     
       if (i!= N){
        for (i=0;i<N;i++){
          if (len2<N){
          str1[i]= '\0';
          }
        str1[i]=str2[i];
        }
       }
      else {
        str1[i]='\0';
    }
      
    printf("%s\n",str1);
    
    }
    it compiles with no problems,but if I run it and put for example "nikos" and "bananas", the result of string1 for N=3 is "banos" instead of "ban". Why does this happen? I thought I null-terminated the string.

    I think my if-else is the problem. I have to check it somehow else.

    _________

    EDIT:
    I think I should be more careful sometimes.Haha.I found my mistake and did like this:
    Code:
    for (i=0; str1[i] != '\0'; i++){
        len1= i;
       }
       for (i=0; str2[i] != '\0'; i++){
        len2= i;
       }
     
       while (i!= N){
        for (i=0;i<N;i++){
          if (len2<N){
          str1[i]= '\0';
          }
        str1[i]=str2[i];
        }
       }
      for (i=N;i<len1;i++){
        str1[i]='\0';
    }
    I'm happy now !
    Last edited by zel; 09-08-2010 at 08:37 AM.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by zel
    Why does this happen? I thought I null-terminated the string.
    I suggest that you indent your code properly. I read:
    Code:
    if (i != N) {
        for (i = 0; i < N; i++) {
            if (len2 < N) {
                str1[i] = '\0';
            }
            str1[i] = str2[i];
        }
    }
    else {
        str1[i] = '\0';
    }
    I do not understand why you compare i != N in the first place: it looks wrong since when i == N, i.e., when the length of the second string is equal to N, you only null terminate the first string at index i, without copying.

    Looking at your for loop, you assign a null character to str1[i] if len < N. But even when this happens, you immediately overwrite it with str2[i].

    I think you should keep things simple: do not determine the length of either of the strings. Rather, just start copying until you reach either the end of the second string, or your counter reaches N, whichever comes first. When that happens, null terminate the first string at that point.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    zel
    zel is offline
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Oh sorry. I didn't see your answer. Is my code now correct?

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by zel
    Is my code now correct?
    Test before asking. Even if it is correct, it looks like a Rube Goldberg machine.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    zel
    zel is offline
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Haha. I see that.

    OK.
    it runs properly except :
    1) that if eg. len2=5 and I give 5 as N,it does no changes.
    and
    2)if eg. len2=5 and I give 10 ,it never goes out of the loop.

    gr..I made it too complicated you think?

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Yes, too complicated. You only need one loop, and you do not even need another if statement.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    zel
    zel is offline
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Ok, it was that simple and I've stuck here for so much time...:$

    Code:
    for (i=0;i<N && str2[i]!='\0';i++){
          str1[i]=str2[i];
        } 
        str1[i]= '\0';
    I don't know why I think so complicated. THANK YOU very MUCH for your help. You really made me think. Thanks again.It works in each case.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Yes, that is pretty much what I had in mind

    Still, you should indent the code properly, and I recommend adding some whitespace for formatting, e.g.,
    Code:
    for (i = 0; i < N && str2[i] != '\0'; i++) {
        str1[i] = str2[i];
    }
    str1[i] = '\0';
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    zel
    zel is offline
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Yes I'm gonna fix that. Thank u again

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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