Thread: A "wrong type" warning

  1. #1
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12

    A "wrong type" warning

    I am writing a programme..and it's so simple but I get this warning all the time.

    warning format ‘ d’ expects type ‘int ’ but argument 2 has type ‘char *’

    What could have gone wrong?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zel View Post
    I am writing a programme..and it's so simple but I get this warning all the time.

    warning format ‘ d’ expects type ‘int ’ but argument 2 has type ‘char *’

    What could have gone wrong?
    Just about anything! Show us your code so we can give you a real answer...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zel
    warning format ‘ d’ expects type ‘int ’ but argument 2 has type ‘char *’

    What could have gone wrong?
    The warning explains it
    Basically, you probably wrote something like this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
        printf("My name is %d\n", argv[0]);
        return 0;
    }
    So the compiler complains that the argument corresponding to %d is not an integer. How to fix this depends on what you are trying to do.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    oops..ok I found the mistake thx 2 u. nice

  5. #5
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Ok. How about this one?

    My warnings are:

    In function ‘main’:
    line 11: warning: assignment makes integer from pointer without a cast
    line 12: warning: assignment makes integer from pointer without a cast
    line 20: warning: assignment makes integer from pointer without a cast
    line 21: warning: assignment makes integer from pointer without a cast



    Code:
    1      #include <stdio.h>
    2.     #include <string.h>
    3.     #include <stdlib.h>
    4.     
    5.     int main (int argc,char *argv[]){
    6.       
    7.      char x,y;
    8.      char *pX, *pY;
    9.      char str1[20],str2[20];
    10.      
    11.     x = (char *) malloc(20* sizeof(char) );
    12.     y = (char *) malloc(20* sizeof(char) );
    13.      
    14.     scanf("%19s",str1);
    15.     scanf("%19s",str2);
    16.      
    17.     pX=&x;
    18.     pY=&y;
    19.      
    20.     x=&str1;
    21.     y=&str2;
    22.      
    23.     printf("x= %c and y= %c", *pX,*pY);
    24.        
    25.     return (0);
    26.       
    27.}
    the purpose of the program is this:
    Write a program which:
    -declares 2 variables of pointer-to-character-type
    -holds dynamic memory for 20 positions of memory for each variable
    -initializes the variables by reading 2 strings from keyboard
    -prints the values of the 2 variables on screen.

    What am I doing wrong? Can u help me understand it?
    Last edited by zel; 09-07-2010 at 11:58 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, why are you trying to assign the return value of malloc to a non-pointer variable? Likewise, why are you trying to assign the address of an array to a non-pointer variable?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    so it has to be:

    [CODE][
    pX = (char *) malloc(20* sizeof(char) );
    pY = (char *) malloc(20* sizeof(char) );
    /CODE]

    right?

    in the next lines should it also be:

    Code:
    //pX=&x;
     //pY=&y;
     
     pX=&str1;
     pY=&str2;
    ?

    how i'm going to print the value of str1 and str2 by giving it in the variables pX and pY ?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You only need pX and pY. After you have allocated space with malloc and assigned the return values of the malloc calls to them, they would point to the first character of a dynamic array of char, which you can thus use as a string.

    Remember to free what you malloc.

    (Of course, if you are always allocating exactly 20 characters, then you will find that dynamic memory allocation is quite unnecessary.)
    Last edited by laserlight; 09-07-2010 at 12:29 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    ok I also added 2 free.
    Code:
    free(pX);
    free(pY);
    I'm confused at the 2nd part of your answer. you mean I don't have to use str1 and str2 but pX and pY instead?
    ps.sorry if my English is bad

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zel
    you mean I don't have to use str1 and str2 but pX and pY instead?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    char *px = malloc(20 * sizeof(char));

  12. #12
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    Ok problem solved. Thank u again

  13. #13
    Registered User zel's Avatar
    Join Date
    Sep 2010
    Posts
    12
    ok here's another problem. I want to copy the first N characters from a string1 to a string2 and erase the rest characters of string1 BUT without using any string function from <string.h>.

    So if
    string1= bananas
    string2=boat
    and N=2

    at the end it will be:
    string1=bo
    string2=boat

    how can I make this happen?
    I found the length of string1 and then I want to swap the substring with the string1..But I'm stuck here.Any ideas?

    Code:
    void copy( char* str1,char* str2, int N) {
    
     int i,len1;
     char *temp;
     for (i=0; str1[i] != '\0'; i++){
        len1= i;
       } 
     
     for (i=0;i<N;i++){
        
      *temp=*str1;
      *str1=*str2
      *str2=temp;
    }
    printf("%19s\n",str1);
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, if you regard your code, temp is uninitialized. Points to neverland.
    But let's formulate an algorithm for you want to do and start from there:

    Truncate string 1.
    Copy N characters from string 2 to string 1.
    Null terminate string 1 (otherwise it won't be a string.)

    Try that.
    Also, printf does not need the size of the string to print it. So %19s should be %s.
    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.

  15. #15
    Novice
    Join Date
    Jul 2009
    Posts
    568
    You might want to look at the documentation of strncpy() for inspiration. Pay attention to it's function parameters, and description of how it works. This is what you are trying to implement.

    Have fun with reverse engineering.

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, 01: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