Return A Char

PS:Sorry For my Eng

I want to write a function that returns a char how can i do this?


when i try this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char* kucuk(char str1[])
{
char str2[256];
for(int i = 0; i < sizeof(str1); i++)  
{
str2[i] = str1[i];
}
for(int i = 0; i < sizeof(str2); i++)  
{
str2[i] = tolower(str2[i]);
}
//printf("%s",str2);
return str2;
}

void oyaya ()
{

cout<<kucuk("oyayAaAAa");

}



output is

1
2
3
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠@ÿXU☺╠╠╠╠╠╠╠╠╠╠╠        ╠╠╠╠╠╠╠╠ì       ╠╠╠╠╠╠╠╠




waiting for help, thanks.
Last edited on
The function you have written returns a pointer. A pointer is not a char.

char* kucuk(char str1[]) returns a char*, which is a pointer.

char kucuk(char str1[]) returns a char.

Do you actually want to return a string? C++ has far better ways to deal with strings than using char pointers.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

std::string kucuk(std::string input)
{
std::string str2;
for(int i = 0; i < input.length(); i++)  
{
str2.push_back(input[i]);
}
for(int i = 0; i < str2.length(); i++)  
{
str2[i] = tolower(str2[i]);
}
return str2;
}

int main ()
{

  std::cout<<kucuk("oyayAaAAa");
  return 0;
}
Last edited on
Firstly, your trying to return a char* here, not a char. There is a big difference.
problem 1: In c++ arrays are passed to functions as pointers, so str1 is a char* and thus sizeof(str1) = 4. Note that sizeof(str2) is 256 as you would expect.
problem 2: char array str2 is a local variable. It exists in this function and it will not exist when the function ends (that is, its used memory will be reused). What you should do is use dynamic memory (see the tutorial, if you haven't). However then in this code, you'll have no way of deleting it. You should (always if possible) use std::string to take care of the memory management (see the reference).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
char* kucuk(char str1[])
{

static char str2[256];
strcpy (str2,str1);
for(int i = 0; i < sizeof(str2); i++)  
{
str2[i] = tolower(str2[i]);
}

return str2;
}



void oyaya ()
{
	char oya[]="OyaOyayayYax";

printf("%s",kucuk(oya));
	
}



with this code it work properly? or does it?

i want do this with char because i want learn

and i have question, what is diffrence with char and char*?
and i have question, what is diffrence with char and char*?

A char is a data type used to represent a single character.

A char* is a pointer. It is just a number. The number is the number of somewhere in memory.

You need to go back to basics and learn the basic types; int, char, double, int*, double*, and so on. If you do not know the difference between basic types, you will never get anywhere.
Last edited on
Yes, now it should work. Unless you try
1
2
3
char* a = kucuk("hello");
char* b = kucuk("world");
std::cout << a << " " << b;
- here you'll get "world world". That's because since str2 is static, every time you call kucuk, the same memory will be used and thus new words will overwrite the old ones.
Last edited on
yes, i got it :D. But if we do

delete [] str2; under return, it is working.
anyway
thanks for helps
it is working.

No. You can't delete[] an array that is on the stack.
No new[], no delete[], simple.
Topic archived. No new replies allowed.