Thread: str length of int array

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

    str length of int array

    So how would I go about getting the string length,basically what I'm trying to do is write a recursive program that takes a number from user and then prints those numbers out vertically recursively. I'm just getting junk in my output but I thought I could do it this way without going into anything to crazy. Well this is what I have so far, basically in a char array I can use strlen to get the length of the string up to the terminating character, how would I do this for integers?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int vertPrint(int array[5], int length);
    int main()
    {
    int array[5], length=0;
    printf("Enter in a String of Numbers to Print Vertically");
    scanf("%s", &array);
    
    
    
    vertPrint(array, length);
    
    }
    
    int vertPrint(int array[5], int length)
    {
    static int i = 0;
    
    if (i < length+1){
    printf("%d\n", array[i]);
    i++;
    return (vertPrint(array, length));
    }
    else
    return;
    
    }
    ignore the fact that I took out the code for length, I did that when I posted it on here as I had used length= sizeof(array) but that isn't going to work
    Last edited by Sorinx; 11-23-2012 at 06:29 PM.

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    If I change up my code a bit to something like this I get junk too

    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int vertPrint(int array[5]);
    int main()
    {
    int array[5];
    printf("Enter in a String of Numbers to Print Vertically");
    scanf("%s", &array);
    
    
    
    vertPrint(array, length);
    
    }
    
    int vertPrint(int array[5])
    {
    static int i = 0;
    
    if (array[i] != '\0'){
    printf("%d\n", array[i]);
    i++;
    return (vertPrint(array));
    }
    else
    return;
    
    }

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >"Enter in a String of Numbers to print vertically"
    Except a string is not the data structure you use in vertPrint(int array[5])
    Neither is scanf() is being called correctly to fill in a string: &array is not how you pass arrays to scanf().

    I suggest you read this -- FAQ > How do I get a number from the user (C) - Cprogramming.com -- and fill in an integer array, one element at a time.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Huh a string is just an array of values, I can do the same exact code as char, and it will work correctly... and that's all I had to do is change it to char my mistake
    Last edited by Sorinx; 11-23-2012 at 07:02 PM.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Sorinx View Post
    Huh a string is just an array of values, I can do the same exact code as char, and it will work correctly
    A char is just one byte. You can definitely use chars to store small numbers, but in any case, you should not treat integer type variables the same as character types. For example, you're wrong about what a string is. A string is a concept formally defined in the C language as an array of bytes terminated by a zero byte. It is not just an array of values of any kind, like you imply.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Quote Originally Posted by whiteflags View Post
    A char is just one byte. You can definitely use chars to store small numbers, but in any case, you should not treat integer type variables the same as character types. For example, you're wrong about what a string is. A string is a concept formally defined in the C language as an array of bytes terminated by a zero byte. It is not just an array of values of any kind, like you imply.
    Really guy, that definition is mind blowing different than my understanding. Thanks for all the help and greatly changing my understanding of what a string is! Also thanks for the solid advice on the problem at hand, your dedication to making an effort instead of a smug response truly helped me! Thanks again guy you are a credit to this site.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    Finished Code may help someone down the line

    Code:
    #include <stdio.h>
    
    
    
    int vertPrint(char array[]); //function prototype
    
    int main()
    {
    char array[100]; //array declaration
    
    printf("Enter in a String of Numbers to Print Vertically:");
    scanf("%s", &array); //user input and string stored in array
    
    
    
    vertPrint(array); //function call
    
    return(0);
    
    }
    
    int vertPrint(char array[]) //function definition
    {
    
    static int i = 0;  
    
    if (array[i] != '\0'){ // if array at pointer i does not equal EOT then printf array at pointer i, add 1 to i, then do the function again, else return to main.
    printf("%c\n", array[i]);
    i++;
    return (vertPrint(array));
    }
    else
    return;
    
    }
    Last edited by Sorinx; 11-23-2012 at 09:09 PM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unfortunately this code still presents a lot of issues. I don't know what compiler you are using, but it absolutely blows.
    Code:
    mingw32-gcc.exe -Wall -ansi  -g  -pedantic -Wextra -Wall -ansi -g -std=c99    -c C:\Users\Josh2\Documents\foo\main.c -o obj\Debug\main.o
    C:\Users\Josh2\Documents\foo\main.c: In function 'main':
    C:\Users\Josh2\Documents\foo\main.c:12: warning: format '%s' expects type 'char *', but argument 2 has type 'char (*)[100]'
    C:\Users\Josh2\Documents\foo\main.c: In function 'vertPrint':
    C:\Users\Josh2\Documents\foo\main.c:33: warning: 'return' with no value, in function returning non-void
    mingw32-g++.exe  -o bin\Debug\foo.exe obj\Debug\main.o    
    Output size is 59.96 KB
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 2 warnings
    Both of these are things you should have been told about by the compiler, and things you ought to fix. I already told you that using & is not how you pass arrays to the scanf() function, and reading the warning tells you why. The type of the argument made is not appropriate.

    Not all recursive functions have meaningful return values, so they don't have return statements, and opt to return nothing. I suspect you did not know this, so you forced the recursion to happen in a return statement. A function calling the same function in a definition is what makes it recursive. Don't force yourself to use return values.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    void vertPrint(int array[5], int length);
    int main()
    {
    int array[5], length=5, i;
    printf("Enter in a String of Numbers to Print Vertically");
    for (i=0;i<=4;i++)
    scanf("%d", &array[i]);
     
     
     
    vertPrint(array, length);
     return 0;
    }
     
    void vertPrint(int array[5], int length)
    {
    int i = 0; 
    while (i <= length-1)
    {
    printf("%d\n", array[i]);
    i++;
    }
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sorinx
    Finished Code may help someone down the line
    It may be finished to you, but there are still lots of room for improvement besides whiteflags' concerns:
    • You need to indent the code properly.
    • Although you say "a String of Numbers", the input could validly be other text, e.g., a name.
    • Your use of %s to read into the string is vulnerable to buffer overflow. It should be %99s instead, or you should use fgets instead of scanf.
    • Your vertPrint function is not reusable because there is no way to reset i to 0. Rather, i should be a parameter, or you can make use of pointer arithmetic with the array parameter, which is really a pointer to char.


    Quote Originally Posted by justine
    Try this:
    You also need to indent your code properly. I recommend comparing i < 5 instead of i <= 4, and in fact 5 really should be a named constant or something instead. Besides this, Sorinx's point was to use recursion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    @laserlight
    Sorry, I didn't notice that, I am a beginner.
    Can I ask Is op's question is valid ?

    Thanks.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by justine View Post
    Can I ask Is op's question is valid ?

    Thanks.
    What?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by justine
    Can I ask Is op's question is valid ?
    You're asking if Sorinx's question is valid? I would say yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Array length
    By otaconBot in forum C Programming
    Replies: 4
    Last Post: 08-08-2012, 03:08 PM
  2. c++ array length?
    By corbinc in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2009, 02:11 PM
  3. Getting the length of an array
    By Pharao in forum C Programming
    Replies: 24
    Last Post: 09-05-2008, 01:53 PM
  4. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  5. length of an array
    By chrismiceli in forum C Programming
    Replies: 8
    Last Post: 02-27-2003, 11:18 PM