Thread: This must be simple

  1. #1
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55

    This must be simple

    I'm just trying to get this code to print but it won't compile and I just can't see it, alittle help?

    Code:
    #include <stdio.h>
    
    void mystery(char *str)
    {
    	if(*str != '\0')
    	{
    		mystery(str + 1);
    		printf("%s\n", str);
    	}
    }
    mystery("abcd");
    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Is this your entire source code? Where is your main function? You also appear to have an incorrect understanding of how C-strings work. What exactly do you want your program to do? Please remember to always include exactly what errors, warning, and/or other message you get when your code does not compile.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55
    Yeah, that's all of it, well, I think it is.

    The errors I get are:

    error C2501: 'mystery' : missing storage-class or type specifiers
    error C2365: 'mystery' : redefinition; previous definition was a 'function'
    error C2440: 'initializing' : cannot convert from 'const char [5]' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you can't call a function outside of main it thinks you are trying to reinitialize it. There are your first 2 errors. The last one we can't tell, because we don't have all the code.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Every program needs a main function - it's where the computer will start executing your program.

    Code:
    int main(int argc, char *argv[])
    {
    // ... your main execution code
    }
    The return type of main should be an int. Some people use void main, but some people are losers. All the stuff in parentheses is how you can get parameters into your program from the OS. You can leave this blank if you want to. Just be aware that it's there, as some people will use it.

    You can have all of your mystery() functions code written before your main function, or you can write it after. If you write it after, you need to add your function's prototype (void mystery(char *str)) followed by a semicolon above your main function.

    Inside your main function, you can make your function call to mystery(). That should get the code to compile. It does not necessarily make your mystery function work, however.

    edit - nevermind what I said about strings in my last post.
    Last edited by sean; 12-13-2004 at 09:58 PM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Location
    Oklahoma City
    Posts
    55
    Got it

    Code:
    #include<stdio.h>
    
    void Mystery(char *);
    
    int main(void)
    {
        Mystery("abcd");
    }
    
    void Mystery(char* Str)
    {
        if (*Str != '\0')
        {
            Mystery(Str + 1);
            printf("%s\n",Str);
        }
    }

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    unless it's for educational purposes, i suggest not using recursion.
    you could get the same effect with

    Code:
    #include<stdio.h>
    
    void Mystery(char *);
    
    int main(void)
    {
        Mystery("abcd");
    }
    
    void Mystery(char* Str)
    {
        while(*Str != '\0')
        {
              printf("%s\n",Str);
              Str++;
        }
    }
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    actually yours and SpudNuts' produce different results misplaced:
    Mystery1 = SpudNuts
    Mystery2 = misplaced
    Code:
    #include<stdio.h>
    
    void Mystery1(char *);
    void Mystery2(char *);
    
    int main(void)
    {
        puts("First:");
        Mystery1("abcd");
        puts("\nSecond:");
        Mystery2("abcd");
        return 0;
    }
    
    void Mystery1(char* Str)
    {
        if (*Str != '\0')
        {
            Mystery1(Str + 1);
            printf("%s\n",Str);
        }
    }
    
    
    
    void Mystery2(char* Str)
    {
        while(*Str != '\0')
        {
              printf("%s\n",Str);
              Str++;
        }
    }
    output
    First:
    d
    cd
    bcd
    abcd

    Second:
    abcd
    bcd
    cd
    d
    Mine:
    Code:
    void Mystery3(char* Str)
    {
      char *end = NULL;
      for(end = Str; *end != '\0'; end++);
      for(end--; end >= Str; end--)
        puts(end);
    }
    Last edited by Thantos; 12-18-2004 at 11:52 PM.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    my bad....i didn't unwind the stack in my head..either than or i was thinking mystery was called before printf
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Heh I think the only reason I caught it was because I had to write a recursive str_rev() function for my C++ final. This basically does the same just the print it different.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    my question is, why isn't there a std str rev function (pre c++)?

    or is there and i've completely missed it somehow?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Why ask why, try bud dry

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why isn't there a std str rev function (pre c++)?
    Because it's dead easy to write, and not as useful as the other string handling functions (in the standard library) that are also dead easy to write.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM