LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using *strng vice *(strng + i) (https://www.linuxquestions.org/questions/programming-9/using-%2Astrng-vice-%2A-strng-i-208975/)

ripwheels8 07-23-2004 11:28 PM

Using *strng vice *(strng + i)
 
Can someone tell me please why this does not work:
#include <stdio.h>

void display (char []);

int main()
{
char message[] = "Vacation is near";

display (message);

return 0;
}

void display (char*strng)
{
int i = 0++;
while (*strng != '\0', 0++)
{
printf("%c", *strng);
++i;
}
printf("\n");
return;
}

I receive the Errors:
17: Lvalue required in function display(char *)
18: Lvalue required in function display(char *)

Help is much appreciated.
Mike

paulsm4 07-23-2004 11:56 PM

A few corrections...
 
Hi -

Here is the corrected version:
Code:

#include <stdio.h>

void display (char []);

int main()
{
  char message[] = "Vacation is near";

  display (message);

  return 0;
}

void display (char*strng)
{
  while (*strng)
  {
    printf("%c", *strng);
    strng++;
  }
  printf("\n");
}

Here were the problems:

1. You can't increment a literal constant like "0"
<= THIS WAS THE PROBLEM THAT CAUSED THE
TWO COMPILER ERRORS

2. You didn't need an array index, you could just increment the
pointer variable directly (this is the approach I took above).

Here's the same code using an index:
Code:

void display (char*strng)
{
  int i = 0;
  while (strng[i])
  {
    printf("%c", strng[i]);
    i++;
  }
  printf("\n");
}

3. Finally, when posting code on LinuxQuestions.org, be sure to take advantage of the different vbCode tags that are available to you: bold, italic, http ... and, in this case, the "code" tag.

Hope that helps .. PSM

(PS: You'll notice that I *didn't* take advantage of the "list" tag. Or the "italic" tag, either ;-)


All times are GMT -5. The time now is 06:04 AM.