C string mysteriously changes value

A do/while loop is somehow changing a string in my code

Page 1 of 1

2 Replies - 1025 Views - Last Post: 19 October 2008 - 07:57 PM Rate Topic: -----

#1 Arkay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-October 08

C string mysteriously changes value

Posted 19 October 2008 - 07:42 PM

I'm learning the C language. Currently I'm on Day 10, which has me dealing with strings. The review section asks me to create a program which accepts two strings, and determines the longer one. Now, having cheated and looked to the back, I know there is an easier way, using 'strlen'. However, I want to understand why the code I devised, which doesn't change the first string, does change the second.

/* This program accepts two strings from the user, counts the number of *
 * characters in each string, and returns a pointer to the longer string*/

#include <stdio.h>
#include <stdlib.h>

int result = 0;

int compare(char *, char *);

int main (void)
{
  char first[50], second[50];


  /* Takes two strings from the user */
  system("clear");
  printf("Please enter two names.\n");
  printf("After each string, press enter.\n");
  scanf("%s", first);
  scanf("%s", second);

  //Troubleshooting line
 printf("\nThe first name you entere is %s\n", first);
  printf("\nThe second name you entered is: %s\n", second);
  //End troubleshooting

  result = compare(first, second);

  if (result == 1)
  {printf("\nThe longer name is: %s\n", first);}
  if (result == 2)
  {printf("\nThe longer name is %s\n", second);}
  return 0;
}

int compare(char * first, char * second)
{
  //Troubleshooting line
  printf("\nComing into the function, the first name is: %s\n", first);
  printf("\nComing into the function, the second name is: %s\n", second);
 //End troubleshooting

  char *p, *p2;
  int one=0, two=0, count = 0;
  int result = 0;

  do
  {
	*p = first[count];
	count++;
	one++;
  } while ( *p != '\0');

  /* Troubleshooting; checking variables */
  printf("\nAfter the first while loop, first is %s", first);
  printf("\nAfter the first while loop, second is %s", second);
  count = 0;
  printf("\nThe counter has been reset to %d", count);
  /* End Troubleshooting check */
  do
  {
	*p2 = second[count];
	count++;
	two++;
  } while (*p2 != '\0');

  //Troubleshooting
  printf("\nAfter the second while loop, first is %s", first);
  printf("\nAfter the second while loop, second is %s", second);
  printf("\nThe first name is: %s", first);
  printf("\nThe second name is: %s", second);
  printf("\nThe variables one & two are %d & %d", one, two);
  //End troubleshooting


  if (one > two);
  {
  result = 1;
  return result;
  }
  if (one < two);
  {
  result = 2;
  return result;
  }
}



I link the entire program only because I don't know where this error is. The troubleshooting checks isolate the error to the second do/while loop in the compare function, however. Before the loop, the string 'second' is correct; afterwards the output of 'second' is garbage.

The error, hopefully, is something glaringly obvious. I want to understand why this change is happening; please help?

Is This A Good Question/Topic? 0
  • +

Replies To: C string mysteriously changes value

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,219
  • Joined: 23-August 08

Re: C string mysteriously changes value

Posted 19 October 2008 - 07:51 PM

I think you want
*p = first[count];
to be
p = &first[count];
and same for p2.
Was This Post Helpful? 0
  • +
  • -

#3 Arkay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-October 08

Re: C string mysteriously changes value

Posted 19 October 2008 - 07:57 PM

View PostJackOfAllTrades, on 19 Oct, 2008 - 07:51 PM, said:

I think you want
*p = first[count];
to be
p = &first[count];
and same for p2.

Wow, that fixed the problem; now my string outputs correctly! Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1