quick strlen question

This is a discussion on quick strlen question within the C Programming forums, part of the General Programming Boards category; I'm trying to input a string from user into an array, but I can't do what I'm trying to do. ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    126

    quick strlen question

    I'm trying to input a string from user into an array, but I can't do what I'm trying to do. I know it's an easy fix but never done this before

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    
    char phrase;
    int i, length;
    
    
    printf("Enter in a word to compare");
    scanf("%s", &phrase);
    
    length = strlen(phrase);
    
    char array[length];
    
    }
    Last edited by Sorinx; 11-20-2012 at 02:26 PM.

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    phrase also needs to be an array, not a single character
    Code:
    char phrase[20]; // max 19 letters + terminating character
    
    scanf("%s", phrase);
    Last edited by DRK; 11-20-2012 at 02:27 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Ok but my issue is with strlength itself, I understand how arrays work but it says making pointer from integer without a cast. I'm guessing because phrase is char when I'm trying to store the length in an int to then create the size of the array.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Exactly.
    If you look the prototype of strlen it needs a char* as parameter, but you pass a char , thus "making pointer from integer without a cast" .
    You need to pass a string, not just a character.I mean it's logical , isn't? Why would you like to find the length of ONE character?

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Yeah I didn't even notice I forgot [] on phrase.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32
    Quote Originally Posted by Sorinx View Post
    I'm trying to input a string from user into an array, but I can't do what I'm trying to do. I know it's an easy fix but never done this before

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    
    char phrase;
    int i, length;
    
    
    printf("Enter in a word to compare");
    scanf("%s", &phrase);
    
    length = strlen(phrase);
    
    char array[length];
    
    }
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
    
    char phrase[1024]; /* you have to allocate space for string input
                                  * I've allocated 1024 bytes or 1 Kebibyte
                                  * for input because there's no way to
                                  * know the length of input from functions like
                                  * scanf... we allocate 1 kebibyte and hope that 
                                  * the user doesn't know a really big word or
                                  * wants to exploit a buffer overflow
                                  */
    int i, length;
    
    
    printf("Enter in a word to compare");
    scanf("%s", &phrase);
    
    /*
    length = strlen(phrase);
    
    char array[length];
    
    The code you entered above is illegal... you can't allocate space
    for an array like that after a declaration... if you had allocated space
    like I had above, then it's kosher... but after the declaration, it's not legal
    
    Also look into malloc() and realloc() functions for allocating space
    dynamically during execution
    */
    
    printf("You entered the phrase: %s\n",phrase);
    
    /* You must return a value from main() or indeed any function
     * for which you've defined with a return type of anything but void */
    return EXIT_SUCCESS;
    
    }
    Last edited by twiki; 11-20-2012 at 02:59 PM.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You can not declare an array without specifying the size and without initializing the array at the same time.

    For example
    Code:
    #include <stdio.h>
    
    int main(void)
    {
         char phrase[];
    
         scanf("%s" , phrase );
    
         return 0;
    }
    will produce an elegant error
    Code:
    Macintosh-c8bcc88e5669-9:~ usi$ gcc -Wall px.c -o px
    px.c: In function ‘main’:
    px.c:5: error: array size missing in ‘phrase’

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Its perfectly legal to declare an array whose size is a variable expression. This is known as a variable-length array.

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char buf[1000] = "";
        int length;
          
        printf("Enter in a word to compare: ");
        scanf("%999s", buf);
         
        length = strlen(buf);
        char array[length];
        printf("Initialized array with %d elements\n", 
                sizeof(array)/sizeof(*array));
        return 0; 
    }

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    This is what I was trying to do I finished

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    char upper(char array[]);
    char palindrome(char array[], int length);//function prototypes
    
    int main(void)
    {
    
    char array[200];
    int length, result=5;//char declarations
    
    printf("Enter in a word to compare");//scans user input word to char array
    scanf("%s", &array);
    
    
    length = strlen(array); //gets string length of array
    
    upper(array);
    result=palindrome(array,length);//function call for uppercase, and stores returns for palindrome in result
    
        if(result==1)
            printf("%s, is a palindrome", array);
        else
            printf("%s, is not a palindrome", array);// if result = 1 it's a palindrom, else it is not
    
    getchar();
    
    }
    
    char upper(char array[]) //upper case function definition
    {
    int i=0;
    
        while (array[i] != '\0'){ //while array[i] does not = end of string make array[i] uppercase, then increase pointer
            array[i]=toupper(array[i]);
            ++i;
                                }
    
    }
    
    char palindrome(char array[], int length)//palindrom function definition passing array and length
    {
    
    
        if(length<2) //if length of string is less than 2 return 1
            return 1;
            
        if (array[0] != array[length-1]) // if array at pointer 0 is not equal to array at length -1 return 0
            return 0;
            else           
            return palindrome (array+1, length-2); //if they are equal call the function again
    
    }

  10. #10
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32
    Quote Originally Posted by c99tutorial View Post
    Its perfectly legal to declare an array whose size is a variable expression. This is known as a variable-length array.

    Code:
    #include <stdio.h>
    #include <string.h>
     
    int main(void)
    {
        char buf[1000] = "";
        int length;
          
        printf("Enter in a word to compare: ");
        scanf("%999s", buf);
         
        length = strlen(buf);
        char array[length];
        printf("Initialized array with %d elements\n", 
                sizeof(array)/sizeof(*array));
        return 0; 
    }

    Yeah. My mistake. I read it as char phrase[length] where phrase was already declared at the top of main as a single character. I didn't notice that it was a new character array named appropriately enough array

    I didn't think about the limit in the format string either... Excellent point!
    Last edited by twiki; 11-20-2012 at 04:59 PM.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Sorinx View Post
    This is what I was trying to do I finished
    A project never finishes. You just go to another . Always there is something to augment.

    About the code i got these warnings
    Code:
    px.c: In function ‘main’:
    px.c:15: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
    px.c:15: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[200]’
    px.c:30: warning: control reaches end of non-void function
    px.c: In function ‘upper’:
    px.c:41: warning: control reaches end of non-void function
    The compiler explains everything nicely .The px.c:30: warning: control reaches end of non-void function is that you have to write exactly before mains exits return 0;

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by Sorinx View Post
    This is what I was trying to do I finished

    Code:
    char array[200];
    // ...
    scanf("%s", &array);
    Your problem is in scanf. You should take off the ampersand. %s expects an address of a string and the array name on its own is already an address.

    Also turn on warnings when you compile and the compiler will tell you this already. Even an expert can make such mistakes but the compiler will help you a lot in finding them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Question
    By Annonymous in forum C# Programming
    Replies: 2
    Last Post: 09-14-2011, 01:23 PM
  2. Replies: 5
    Last Post: 06-09-2006, 07:49 PM
  3. strlen question?
    By Hoser83 in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 07:20 AM
  4. Quick question
    By Stiks in forum C Programming
    Replies: 6
    Last Post: 10-10-2005, 09:35 PM
  5. Quick Question
    By BrianD in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2002, 03:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21