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

In summary, the code includes preprocessor directives, defines constants, declares a prototype, and calls a main function. The code has warnings related to passing arguments. The code declares variables and checks if an array element is a letter. If it is, it upper cases the letter.
  • #1
SnakeDoc
27
1

Homework Statement


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)
^

Homework Equations

The Attempt at a Solution


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:
Physics news on Phys.org
  • #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])
 
  • #3
SnakeDoc said:
: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]);
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.
SnakeDoc said:
C:
  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]
  }
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?
SnakeDoc said:
C:
  if (k == false)
[INDENT]  printf("The line is not a palindrome.");[/INDENT]
  else if(k == true)
[INDENT]  printf("The line is a palindrome.");[/INDENT]
If the first comparison (k == false) is not true, can there be any other possibilities than k == true?
 
  • #4
Khashishi said:
Should be
Should probably be
bool is_palindrome(int n, char *array)
not
bool is_palindrome(int n, char array[n])
bool is_palindrome(int n, char array[]) would also be valid.
 
  • #5
SnakeDoc said:
: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]);
DrClaude said:
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.
SnakeDoc said:
C:
  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]
  }
DrClaude said:
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?
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.​
 
  • #6
Mark44 said:
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.
o:)
 
  • #7
DrClaude said:
o:)
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.)
 
  • #8
Mark44 said:
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.​
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.
 
  • #9
SnakeDoc said:
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.
Instead of //Declare variables, which is pretty useless, maybe do something like this:
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.
C:
   if (is_palindrome(j, letters[]))
   {
      // Do something
   }
   else
   {
      // Do something else
   }
SnakeDoc said:
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.
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.
SnakeDoc said:
usually the lack of it causes us to get points docked.
I'm not at all saying to not use comments. What I'm saying is make an effort to make your comments useful.
 

1. What does "Pointers -->(makes integer from pointer without cast)" mean?

"Pointers -->(makes integer from pointer without cast)" is a common error message in programming that indicates an attempt to convert a pointer into an integer without explicitly casting it. This can lead to unexpected behavior and should be avoided in most cases.

2. Why is converting a pointer to an integer without casting a problem?

Converting a pointer to an integer without casting can be a problem because pointers and integers are two different data types with different sizes and representations in memory. Converting a pointer to an integer without casting can result in the loss of important information and lead to errors in the program.

3. How can I fix the "Pointers -->(makes integer from pointer without cast)" error?

The best way to fix this error is to explicitly cast the pointer to the desired integer type using the appropriate syntax. For example, if you want to convert a pointer to an integer, you can use the syntax "(int) pointer". This ensures that the conversion is done correctly and avoids any potential issues.

4. Can I always convert a pointer to an integer without casting?

No, you should not convert a pointer to an integer without casting in most cases. The only exception is when the pointer is pointing to a memory address that was previously stored as an integer, such as in a memory-mapped I/O situation. In all other cases, it is best to explicitly cast the pointer to the desired integer type.

5. Are there any other common errors related to pointers?

Yes, there are several common errors related to pointers, such as dereferencing a null pointer, using an uninitialized pointer, or attempting to free a pointer that was not dynamically allocated. It is important to understand how pointers work and how to use them correctly to avoid these common errors in programming.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
891
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top