Error "makes pointer from integer without a cast"

This is a discussion on Error "makes pointer from integer without a cast" within the C Programming forums, part of the General Programming Boards category; I get an error saying "makes pointer from integer without a cast" I'm asking the user to enter a char ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Error "makes pointer from integer without a cast"

    I get an error saying "makes pointer from integer without a cast"

    I'm asking the user to enter a char to choose the base. I know my readStr is correct because that is what was given by the teacher

    readStr = In its simple form, this function takes one argument, the address of a char array where a C-style text string can be stored.

    Code:
    #include <stdio.h>
    #include "readStr.h"
    #include "writeStr.h"
    
    #define BUFFER_SIZE 33
    #define HEX_SIZE 9
    #define BIN_SIZE 33
    #define DEC_SIZE 11
    #define BASE_SIZE 1
    
    
    int main()
    {
    
    char input[BUFFER_SIZE];
    char base[1];
    unsigned int x;
    
    writeStr("\nEnter the base of the value to be converted");
    writeStr("\n d-decimal, h-hexadecimal, b-binary: ");
    
    readStr(base, BASE_SIZE);
    
    if(base =='d' || base =='D')
    writeStr("Decimal");
    
    if(base =='h' || base =='H')
    writeStr("Hex");
    
    if(base =='b' || base =='B')
    writeStr("Binary");
    
    
    
    return 0;
    }
    ~
    Code:
     
    #include <stdio.h>
    #include "readStr.h"
    #include <stdlib.h>
    #include <unistd.h>
    
    int readStr(char *theString, int limit)
    {
    unsigned int length =0;
    limit--;
    
    read(STDIN_FILENO, theString, 1);
    
    while((*theString != '\n') && (length < limit))
      {
      theString++;
      length++;
    
      read(STDIN_FILENO, theString, 1);
      }
    while (*theString !='\n')
      read(STDIN_FILENO, theString, 1);
      *theString= '\0';
    
    
    return length;
    
    }
    ~

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I know my readStr is correct because that is what was given by the teacher
    Ha ha... such a sweet kid...

    What line number does it say the error is occurring on? Much easier to find that way.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by sean_mackrory
    Ha ha... such a sweet kid...

    What line number does it say the error is occurring on? Much easier to find that way.
    its happen ing at my boolean expressions

    if(base =='d' || base =='D')
    writeStr("Decimal");

    if(base =='h' || base =='H')
    writeStr("Hex");

    if(base =='b' || base =='B')

    I guess I'm comparing a pointer which is "Base" to an interger/char 'd' which i can't. How do i fix that?

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Instead of referring to simply base, which is a pointer, you can refer to base[0], which is a char. Then you'll be comparing chars to char.

    edit:

    Try making base a single char.
    ... or that...

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    Quote Originally Posted by sean_mackrory
    Instead of referring to simply base, which is a pointer, you can refer to base[0], which is a char. Then you'll be comparing chars to char.

    edit:



    ... or that...
    I tried that but for some reason if the user enters a "d" it won't print out Decimal...it doens like my boolean expressions

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What I'd like to know, is who on earth is teaching these people today. This is the second time in as many days I've seen people making arrays hold ONE value. Do they not know the purpose of an array?

    Now this is no slight on the origional poster, rather, whoever is teaching them. Let's get it clear for you, because your teacher apparently cannot:

    An array is a collection of objects of the same type. There is no point in having an array of ONE item. I can see one use for it, but that's a bit of a stretch. (The only possible use would be if you wanted to use the name of the variable as a pointer, and not have to use the address-of operator to do so, but you lose any benifit in doing so by simply having to type the array index of zero with every other single use. So it has no real practical purpose. If you didn't understand that, that's fine, just ignore it, and remember: Arrays are for groups of a type of object.)

    If you have one single instance of a type, say for example, one integer, there is no need at all to ever use it in an array. If you have two or more, and you want them grouped together, then an array is just fine.

    If you're still unclear, let's compare the two of them:
    Code:
    int arrayofone[1];
    int justone;
    
    arrayofone[0] = 10;
    justone = 10;
    
    for( arrayofone[0] = 0; arrayofone[0] < 10; arrayofone[0]++ )
         printf( "Counting with \'arrayofone\': %d\n", arrayofone[0] );
    
    for( justone = 0; justone < 10; justone++ )
        printf( "Counting with \'justone\': %d\n", justone );
    Now in those very simple examples, look at how much easier it is, for a single item, to not be an array.
    1) At declaration, you don't have to waste time adding "[1]" to the end of it.
    2) At assignment time, you don't have to add "[0]" to it to access it.
    3) At all other times, comparison, assignment, incrementing, you don't have to add "[0]" to access it.

    Not only that, it's much cleaner. Because for one, when I see [ ], I tend to think there will be more than one value. Because seriously, who on eath would waste time making an array out of a single object? There's just no point in doing so.

    Again, not a slight on you, but rather, your instructor.

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

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    The reason your code doesn't recognise when the user enters 'd' is that the readStr() function is doing exactly what it says it does. It reads the longest null-terminated string it can fit into the array you pass it. You are passing an array of length 1, so all readStr() can fit into that string is the null terminator '\0'. That is what is being returned to you - everything else is lost. To fix the problem use:
    Code:
    #define BASE_SIZE 2
    ...
    char base[BASE_SIZE]; /* enough for the first entered char and the null terminator */
    ...
    readStr(base, BASE_SIZE):
    ...
    if(base[0] =='d' || base[0] =='D')
    ...
    I hope that makes sense.
    DavT
    -----------------------------------------------

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Thumbs up

    Quote Originally Posted by DavT
    The reason your code doesn't recognise when the user enters 'd' is that the readStr() function is doing exactly what it says it does. It reads the longest null-terminated string it can fit into the array you pass it. You are passing an array of length 1, so all readStr() can fit into that string is the null terminator '\0'. That is what is being returned to you - everything else is lost. To fix the problem use:
    Code:
    #define BASE_SIZE 2
    ...
    char base[BASE_SIZE]; /* enough for the first entered char and the null terminator */
    ...
    readStr(base, BASE_SIZE):
    ...
    if(base[0] =='d' || base[0] =='D')
    ...
    I hope that makes sense.

    yes, thanks for making it clear

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 11:53 AM
  2. Problem with "pointer from integer without a cast"
    By firyace in forum C Programming
    Replies: 6
    Last Post: 05-11-2007, 10:48 AM
  3. comparison between pointer and integer
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-07-2006, 01:15 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM

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