1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pointers -->(makes integer from pointer without cast)

  1. Nov 10, 2015 #1
    1. The problem statement, all variables and given/known data
    I am to write a program that has its user enter 100 character or less and determine if the line is a palindrome or not. I must use pointers one that starts at the beginning and one at the end of the array that must work their way in until they meet.(I'm also having trouble with this not sure how to keep them from going past each other.) As of now the program seems to be working just fine. When I compile the code I get the following warnings though.

    :45:5: warning: passing argument 2 of 'is_palindrome' makes pointer from integer without a cast [enabled by default]
    k = is_palindrome(j, letters[N]);
    ^
    :20:6: note: expected 'char *' but argument is of type 'char'
    bool is_palindrome(int n, char array[n]);
    ^
    In function 'is_palindrome':
    hw27.c:61:19: warning: assignment makes pointer from integer without a cast [enabled by default] while(arrayp1 =! arrayp2)
    ^
    :65:25: warning: assignment makes pointer from integer without a cast [enabled by default]
    if( arrayp1 =! arrayp2)
    ^


    2. Relevant equations


    3. The attempt at a solution
    Code (C):

    //Preprocessor Directives
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <ctype.h>

    //define constants
    #define N 101

    //declare prototype
    bool is_palindrome(int n, char array[n]);

    //call main function
    int main(void)
    {
      // Declare variables
      char letters[N]={'\0'};

      int i=0;
      int j;
      bool k;

      printf("Enter a message: ");

      while ((letters[I]=getchar()) != '\n')
      {
    [INDENT]  //if the array element is a letter then make it upper case
      if (isalpha(letters[I]))
      {
    [INDENT]  letters[I]= toupper(letters[I]);
      i++;
      j++;[/INDENT]
      }[/INDENT]
      }

      k = is_palindrome(j, letters[N]);

      if (k == false)
    [INDENT]  printf("The line is not a palindrome.");[/INDENT]
      else if(k == true)
    [INDENT]  printf("The line is a palindrome.");[/INDENT]

      exit(EXIT_SUCCESS);
    }
    bool is_palindrome(int n, char array[n])
    {
      int i=0;
      int len = n;
      char *arrayp1 = &array[I];
      char *arrayp2 = &array[n-1];

      while(arrayp1 =! arrayp2)
      {
    [INDENT]  for(i=0; i<len; i++)
      {
    [INDENT]  if( arrayp1 =! arrayp2)
      {
    [INDENT]  return false;[/INDENT]
      }
      n--;[/INDENT]
      }[/INDENT]
      }
      return true;
    }
     
    Last edited: Nov 10, 2015
  2. jcsd
  3. Nov 10, 2015 #2
    Should be
    while(arrayp1 != arrayp2)
    not
    while(arrayp1 =! arrayp2)

    Should probably be
    bool is_palindrome(int n, char *array)
    not
    bool is_palindrome(int n, char array[n])
     
  4. Nov 10, 2015 #3

    DrClaude

    User Avatar

    Staff: Mentor

    Using letters[N] is incorrect. That is equivalent to passing a single character, not an array of characters. You need to pass the pointer letters.


    You are using I instead of i. Why to you have a j++ in there? What happens if the user inputs more than 100 characters?



    If the first comparison (k == false) is not true, can there be any other possibilities than k == true?
     
  5. Nov 10, 2015 #4

    DrClaude

    User Avatar

    Staff: Mentor

    bool is_palindrome(int n, char array[]) would also be valid.
     
  6. Nov 10, 2015 #5

    Mark44

    Staff: Mentor


    This change in case is an artifact of the forum software. If you type "letters[i]", the software things it is a BBCode italics tag, and changes it to [I] and renders everything following it in italics, even inside a block with code tags.

    @SnakeDoc, what is with the
    tags in your code?

    Also, while I'm at it, some of your comments look like they're there only because some comments are required.
    For example //define constants is merely restating the obvious, that a constant is being #def -ined. A better comment would say what N is being used for.
    //call main function is both useless and technically wrong. This is obviously the main function, but you aren't calling it -- this is the definition of main.
    //Declare variables -- is also pretty useless. A good comment doesn't merely restate what the declaration or statement is doing, but should give some explanation of why this is happening.​

    //if the array element is a letter then make it upper case -- This is a good comment. It explains why the code is doing what it is doing.​
     
  7. Nov 10, 2015 #6

    DrClaude

    User Avatar

    Staff: Mentor

    o:)
     
  8. Nov 10, 2015 #7

    Mark44

    Staff: Mentor

    There are several workarounds.
    1. Don't use i as an array index.
    2. If you use i as an array index, insert a space between [ and i, as in [ i].
    3. Changing the color of the text for the index fools the system software so that it doesn't treat it as an italics tag. For example, arr[i]. (You can't notice that the 'i' is a different color unless you use the BBCode editor.)
     
  9. Nov 10, 2015 #8
    The professor makes us comment all the code he has grad students correct our code and check to see if we have comments when ever I think something doesn't really need a comment for example the //Declare variables I seem to get docked points. The //call the main function thing is just what we were told to do at the beginning of the semester and has become a habit everyone's code in class has that in common. usually the lack of it causes us to get points docked.
     
  10. Nov 10, 2015 #9

    Mark44

    Staff: Mentor

    Instead of //Declare variables, which is pretty useless, maybe do something like this:
    Code (C):

    // Variable declarations
      char letters[N]={'\0'}; // Array that will hold the string to check
      int i;  // Loop control variable
      int j; // Not sure what it's being used for
    In your code you increment j, without ever having initialized it. That's a real problem! You also have a variable k that you don't need.
    Code (C):

       if (is_palindrome(j, letters[]))
       {
          // Do something
       }
       else
       {
          // Do something else
       }
     
    It's really silly. Every C or C++ program has a main() function. I don't think I've seen code in "the real world" where they have a comment on main(). Other functions, however, definitely should have explanatory comments. In any case, main() doesn't call itself, as your comment implies. main() is called from startup code that the compiler and linker insert into the executable.
    I'm not at all saying to not use comments. What I'm saying is make an effort to make your comments useful.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Pointers -->(makes integer from pointer without cast)
  1. Weird pointer outcome (Replies: 9)

Loading...