0

Googled around and can't find out what's going wrong here, the pointer gets passed correctly but it's not working.

The program is supposed to find the length of the character array/string.

What's wrong here? Just always give length of zero!

#include <stdio.h>
#include <stdbool.h>

int stringlength(char *); // Declare function in the beggining (because C)

main()
{
    char testString[100]; // Character array where we'll store input from command line (unsafely)
    char *arrayPointer; // Pointer that will point to array so it can be passed to function
    int length; // Integer for length of string
    printf("Please enter in a string: \n");
    scanf("s", &testString[0]); // Get input
    arrayPointer = &testString[0]; // Point the pointer to the array
    printf("Pointer to array %p\n-----------------\n", arrayPointer); // Output pointer
    stringlength(arrayPointer); // And use the function
    printf("Length is %d\n", length); // Output the length of the string...
}

stringlength(char *stringArray)
{
    int i = 0; // Counter variable
    int length = 0; // Length variable
    bool done = false; // Boolean for loop
    while(!done)
    {
        printf("Character is %c\n", stringArray[i]); // Output character
        printf("Memory location %p\n", &stringArray[i]); // Output memory location of character

        if(stringArray[i] == '\x00') // If the current array slot is a null byte we've reached the end of the array
        {
            done = true; // Null byte found, we're all done here
            return length;
        } else {
            length++; // Not a null byte so increment length!
        }
        i++; // Counter for moving forward in array
    }
}

Output of this is:

mandatory@MANDATORY:~/Programming/C$ ./a.out
Please enter in a string: 
testing
Pointer to array 0x7fffc83b75b0
-----------------
Character is    
Memory location 0x7fffc83b75b0
Character is 
Memory location 0x7fffc83b75b1
Length is 0
4
  • 2
    scanf("s", &testString[0]) probably doesn't do what you think it does. Commented May 14, 2013 at 19:01
  • Paraphrase of line 4: main() // but don't declare main correctly, because K&R C Commented May 14, 2013 at 19:01
  • 1
    "Just always give length of zero!" -- When, exactly, did you set length? The one you print, not the one local to stringlength? Or did you think they are the same for some reason? Commented May 14, 2013 at 19:19
  • 1
    Note that all your detailed comments aren't helping you ... if anything they encourage you to write obscure code. Think: when are i and length different? When is done ever false and the loop exits? Thinking about those should lead to much simpler and easier to read code. Commented May 14, 2013 at 19:30

2 Answers 2

3

You have a few problems:

  1. main should be declared to return int:

    int main(void)
    
  2. Your scanf format is wrong. Use:

    scanf("%s", &testString[0]);
    
  3. The signature in your implementation of stringlength() doesn't match the prototype. Make sure it's:

    int stringlength(char *stringArray)
    
  4. stringlength() doesn't return the length. Add:

    return length;
    

    at the end of that function.

  5. You don't assign to length in main(). Change the call to stringlength() to actually use the return value:

    length = stringlength(arrayPointer);
    
  6. main() should return something. Probably 0. Add:

    return 0;
    

    at the end of main().

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

4 Comments

The prototypes do match implicitly. And the function does return length (and cannot fall through the bottom) ... the logic obscures that.
Excellent, thanks for all the help, I'm a complete newbie to C so my code sucks and I apologize for that!
@CarlNorum 7. "You didn't compile using -Wall - do so."
@MandatoryProgrammer It would be worth reading an introductory tutorial for the C language. Things like not knowing how to return or use the return value make people like me mad. There's no excuse nor good reason for not trying to learn the basics yourself before asking trivial questions.
2

I think you want

    scanf("s", &testString[0]); // Get input

to be

    scanf("%s", &testString[0]); // Get input

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.