0

I've missed a week of class in which they covered pointers but I haven't been able to get the notes from class but my HW is due and I still don't understand how to use pointers to pass strings from function to function... below is my code in which I realize the pointers are beyond messed up but I've tried to read other forums but just get lost. Any help is appreciated.

#include <stdio.h>

char* returnInPlace(char);
int palindrom(char, char );

main(void)
{
  char newString[20];

  printf("Enter a string: ");
  scanf("%s",&newString);

  char flippedString[20]=reverseInPlace(newString);
  int palCheck= palindrome(newString, flippedString);

  if (palCheck==0)
    printf("\n\tThe reverse string is %s, so we don't have a palindrome.", flippedString);
  else
    printf("\n\tThe reverse string is %s, so we do have a palindrome.", flippedString);

}

char* reverseInPlace(char newString)
{
  int iterator;
  char flipped[20];
  char *ptr1;
  for(iterator=0;iterator<20;iterator++)
  {
    flipped[iterator]=firstString[19-iterator];
  }
  ptr1=flipped[];
  return *ptr1;
}

int palindrome(char newString, char flippedString)
{
  int iterator;
  int palCheck=1;
  for(iterator=0;iterator<20;iterator++)
  {
     if (firstString[iterator]==secondString[iterator])
      continue;
     else
     {
       palCheck=0;
       break;      
     }  
  }
  return palCheck;
}
3
  • 1
    what problems you are facing with this code? BTW main(void) should be int main(void) Commented Mar 20, 2015 at 13:24
  • return *ptr; means: return the value on the address that ptr contains. Commented Mar 20, 2015 at 13:29
  • there is an undefined firststring[] Commented Mar 20, 2015 at 13:30

1 Answer 1

1

Problem 1

In char* reverseInPlace(char newString), you're using

return *ptr1;

which is wrong. What you may want is

return ptr1;

Problem 2

ptr1=flipped[];

is wrong. Here, you're assigning the base address of a local variable flipped and returning that value. flipped will cease to exist after reverseInPlace() finishes execution. You need to use dynamic memory allocation.


Problem 3

char flippedString[20]=reverseInPlace(newString);

is wrong. You cannot assign an array with = unless as initialization at time of definition.


Problem 4

char* reverseInPlace(char newString)

this function definition looks wrong by seeing the way it is being called. Maybe what you want is

char* reverseInPlace(char* newString)

instead.

......and maybe many more. Strongly suggest to read some good book about Pointers and C basics before jumping into writing code.

Sign up to request clarification or add additional context in comments.

2 Comments

Capitalize the last p in 'problem 4' please ! :)
for Problem 3, that is the first time I am declaring that variable so that's why I assigned it, am I not allowed to do so?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.