0

I am playing with pointers in the K&R book and I wrote this program that swaps integers and measures the length of a string with a pointer. The first part works but my string length function does nothing. The program compiles and runs the first part and then the program stops responding.

    #include <stdio.h>

extern int a2 = 4;
extern int b2 = 5;

void swap(int *px, int *py);
int strlen2(char *s);
//int printLabel(char *thelabel, char newliner);

//int printLabel(char *thelabel, char newliner)
//{
//        int stringlength1=(strlen2(thelabel));
//  return stringlength1;
//}

void swap(int *px, int *py)  /* interchange *px and *py */
   {
       int temp;
       temp = *px;
       *px = *py;
       *py = temp;
   }

int strlen2(char *s)
   {
       int n;
       for (n = 0; *s != '\0', s++;)
           n++;
       return n;
   }

int main()
{
int a=4;
int b=5;
char newliner = '\n';
swap(&a,&b);
swap(&a2,&b2);
printf("%d",a);
printf("%c",newliner);
printf("%d",b);
printf("%c",newliner);
printf("%d",a2);
printf("%c",newliner);
printf("%d",b2);
printf("%c",newliner);
char sumstring[]="boo";
char *labelPtr;
labelPtr = sumstring;
int length = strlen2(labelPtr);
printf("%d",length);
return 0;
}
2
  • Also, try something like printf("%d\n", a); rather than the two printf lines. Also you could collapse statements like printf("%d\n%d\n%d\n", a, b, a2) and so on. Commented Aug 14, 2014 at 21:14
  • Minor: Better to use type size_t: size_t strlen2(char *s) { size_t n; ... and size_t length ... printf("%zu",length); Commented Aug 14, 2014 at 21:41

5 Answers 5

3

The problem is that this:

for (n = 0; *s != '\0', s++;)

is a semi-infinite loop. It checks for the terminating NUL, but then it ignores the result of that comparison and increements s, continuing the loop if it is non-null. Once it gets past the end of the string, the result is undefined behavior, but its likely to either loop forever or crash.

You probably meant

for (n = 0; *s != '\0'; s++)    
Sign up to request clarification or add additional context in comments.

1 Comment

Very strange. In the book, that's how it was shown. Thanks. That fixed it.
0

In the for loop, the second expression is usually a condition, in your case *s != '\0'. The 3rd expression is the increment, where you are supposed to increment the s pointer.

This is working fine:

int strlen2(char *s)
{
    int n;

    for (n = 0; *s != '\0'; s++)
        n++;

    return n;
}

Comments

0

Replace your code with the following:

int strlen2(char *s)
   {
       int n = 0;
       while(s[n] != '\0')
          ++n;
       return n;
   }

Comments

0

looks like a typo in the code. shouldn't this line be:

for (n = 0; *s != '\0'; s++)

instead of

for (n = 0; *s != '\0', s++;)

Comments

0

In this for statmenet

for (n = 0; *s != '\0', s++;)

in the condition part there is used the comma operator

*s != '\0', s++

Its results is the value of the last subexpression that is of s++. As pointer s is not equal to 0 then you get at least very long sycle.

I think you meant instead

for (n = 0; *s != '\0'; s++ )

Comments

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.