c++ - Swap and Return Char Array from the Function -


i know there many questions related question still facing problem. i'm using function swap array mean when input abcd shoudl return dcba. code below check problem. there way pass second empty array reference? swap working fine, array not returning fine. want solve code related [characters array]. don't discuss string etc.

#include <iostream> #include <string.h> #define max 80 using namespace std; char* reversit(const char a[]){     int size=strlen(a)-1;     char b[max];     int i;     for(i=0; i<strlen(a); i++){         b[size--] = a[i];     }     b[i]='\0';       return b; } int main(){      char a[max];     char b[max];     cin.get(a,max);     b=reversit(a);     cout << b;     return 0; } 

first of all, can indeed not return arrays. that's 1 big problem them.

if really want use char[], can pass output array parameter:

void reversit(const char *a, char *b){     int size=strlen(a)-1;     int i;     for(i=0; i<strlen(a); i++){         b[size--] = a[i];     }     b[i]='\0';   } 

note char[] , char* same function argument.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -