0

Here is my code:

#include<stdio.h>
#define MAXLINE 100

/*print the reverse of the input*/

int getline1(char line[], int maxline);
char *reverse(char);

main(){
    int len;
    char line[MAXLINE];
    char *rp;
    while ((len = getline1(line, MAXLINE)) > 0)
            rp = reverse(line);
            printf("%s", *rp);
    return 0;
}
int getline1(char s[], int lim){
    int c, i;

    for (i = 0; (c=getchar()) != EOF && c != '\n'; i++)
            if (i > lim-1)
                    continue;
            else
                    s[i] = c;
    if (c == '\n'){
            s[i] = c;
            i++;
    }
    s[i] = '\0';
    return i;
}
char *reverse(char ca[]){
    int i;
    int i1 = 0;
    char *rp;
    char reversed[MAXLINE];
    for (i = MAXLINE-1; i >= 0; i--){
            reversed[i1] = ca[i];
            i1++;
    }
    rp = reversed;
    return rp;
}

But when I try to compile it, I get the following errors:

reverse.cpp: In function ‘int main()’:
reverse.cpp:14:20: error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
reverse.cpp:7:7: error:   initializing argument 1 of ‘char* reverse(char)’ [-fpermissive]
reverse.cpp:15:19: warning: format ‘%s’ expects argument of type ‘char*’, but argument 2 has type ‘int’ [-Wformat]

I don't have much experience with C++. What am I doing wrong? I just want to make a pointer to a char array and return it.

3
  • 3
    You may be using a C++ compiler but your code is essentially C. Commented Sep 1, 2012 at 5:33
  • 1
    You should either tag this as homework, or C (and remove C++, 'cause there isn't any C++ here). Commented Sep 1, 2012 at 5:44
  • 1
    The fact that you're returning a pointer to a local temporary array can't be emphasized enough. It's a serious bug and you should take DeadMG's advice. Commented Sep 1, 2012 at 5:55

3 Answers 3

3

I just want to make a pointer to a char array and return it.

You appear to want to return a string. That is not a pointer to a char array. Even if your program compiled, you would invoke UB, as you return a pointer to an automatic object- and there are quite a few other runtime errors in your code as well. You got lucky that you also made a compile-time error so the compiler did not accept your program. This C++ program achieves what you intend:

#include <string>
#include <iostream>

std::string reverse(std::string val) {
    return std::string(val.rbegin(), val.rend());
}
int main() {
    std::string str;
    while(std::getline(std::cout, str))
        std::cout << reverse(str);
}

What am I doing wrong?

You're learning C89 intead of C++11. They're really different things.

If you wish to learn to code C++, you must learn std::string, and the rest of the Standard library. You will not get anywhere with char*, char[], and MAGIC_BUFFER_SIZE.

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

2 Comments

Thanks, you had the right idea, but your code did not work. I did manage to get this to work though: #include<iostream> #include<string> /*print the reverse of the input*/ using namespace std; string reverse(string in){ return string(in.rbegin(), in.rend()); } main(){ string in; while (cin >> in) cout << reverse(in); }
Probably got the wrong header for std::getline or something. Your code is functionally identical to mine except for that.
2

You first declare the function prototype

char *reverse(char);

But the actual function is declared as

char *reverse(char ca[])

That's your problem.

Comments

0

What are you trying to achieve ? There are logical errors in the code ...

    while ((len = getline1(line, MAXLINE)) > 0)
        rp = reverse(line);
        printf("%s", *rp);

this part will call reverse on every /n character but printf will never be called... Also you have string of 100 chars and your reverse will put leading char on end of reverse string.. so if you have string of 5 chars you will have garbage on first 95 positions and then 5 chars you need ...

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.