Nonportable pointer conversion

This is a discussion on Nonportable pointer conversion within the C Programming forums, part of the General Programming Boards category; Another Noob Question - My code is not working as i expect it to. In the following code (userAns[0] - ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    Question Nonportable pointer conversion

    Another Noob Question - My code is not working as i expect it to.
    In the following code (userAns[0] - userAns[5] all = 1) and (userAns[6] - userAns[9] all = 0)
    When run i would expect the 0-5 values to return a correct message and the 6-9 values to return wrong, but all values return the question is wrong text?
    The program does run but not correctly and i just noticed that the line " if (userAns[i] == 1) " gets a Nonportable pointer conversion error.
    Being a total noob looking at the code i wrote i dont understand why when userAns[i] has a value of 1 that it does not display question correct.

    What am i doing wrong?

    Code:
    //FUNCTION TO DISPLAY WRONG OR CORRECT
    int score (void)
    {
    int i, correct;
    correct = 0;
    clrscr();
    for (i=0; i<=q; i++)
        {
         printf ("\n%s", userAns[i]);
         if (userAns[i] == 1)
             {
              correct++;
              printf("\n%s\nYou got this question correct", mystr[i]);
             }
             else {
                      printf("\n%s\nYou got this question wrong", mystr[i]);
                     }
             }
    return;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Please post the full code. This is not enough to figure out what's wrong.

    EDIT: Although, I suspect that userAns is declared as char **, meaning an array of string pointers. If that's the case, then the comparison (userAns[i] == 1) is comparing a pointer value on the left with an integer on the right. You probably are trying to check if the string is "1". You should:

    Code:
    if(strcmp(userAns[i], "1") == 0)
    Instead.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    9
    Sweet thank you that works

    I had been searching the net for about an hour before asking. And i did find reference to strcmp but thought it was specificaly for comparing strings to strings like
    Code:
    if (strcmp(userAns[i], newAns[i]) == 1)
    I did try using it in my code but i put

    Code:
    if(strcmp(userAns[i]) == 1)
    and it crashed everytime i tried running the program.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Glad it worked. Be aware that strcmp() returns 0 if the strings match, not 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  2. Pointer array's and ASCII to bit conversion
    By AdamLAN in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2005, 05:55 PM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 2
    Last Post: 02-07-2002, 08:39 AM

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