c string-problem

hi, i want to convert my strings to uppercase strings.but my function does not work.
note:i don't want to use string.h library.
here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <Windows.h>
#include <iostream>

using namespace std;

char* cct(char []);

void main()
{
char *bpl[] = {"cAr","Pen", "hOmE", "coMputeR"};

cout<<cct(bpl[0])<<endl;
cout<<cct(bpl[1])<<endl;
cout<<cct(bpl[2])<<endl;
cout<<cct(bpl[3])<<endl;
}
char* cct(char k[])
{
char m[10];
for(int i=0;i<=(strlen(k)-1);i++)
{
m[i]=toupper((int)k[i]);
}

return m;
}
m is a local variable that becomes invalid as soon as the function cct is left.

So either you allow k to be modified or you pass another buffer that takes the changes (and may be returned)
m is a local variable that becomes invalid as soon as the function cct is left.


There are so many questions on this forum on exactly the same issue, trying to return local variable value back from function!
I think this should work. I have used dynamic memory allocation. For the first version I do not delete
m
but in the second I do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//First version
#include <Windows.h>
#include <iostream>
using namespace std;

char* cct(char []);

void main()
{
 char* bpl[] = {"cAr","Pen", "hOmE", "coMputeR"};
 
 cout << cct(bpl[0]) << endl;
 cout << cct(bpl[1]) << endl;
 cout << cct(bpl[2]) << endl;
 cout << cct(bpl[3]) << endl;
 
}

char* cct(char k[])
{
 char *m = new char[10];
 int i;
 for( i = 0; i < strlen(k); i++)
 {
  m[i] = toupper(int(k[i]));
 }
 m[i] = '\0';
 return m;
 
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//Second version


#include <Windows.h>
#include <iostream>
using namespace std;

char* cct(char []);

void main()
{
 char* bpl[] = {"cAr","Pen", "hOmE", "coMputeR"};
 char * m = cct(bpl[0]);
 cout << m << endl;
 delete m;

 m = cct(bpl[1]);
 cout << m << endl;
 delete m;

 m = cct(bpl[2]);
 cout << m << endl;
 delete m;

 m = cct(bpl[3]);
 cout << m << endl;
 delete m;

 }

char* cct(char k[])
{
 char *m = new char[10];

 int i = 0;
 for(i = 0; i < strlen(k); i++)
 {
  m[i] = toupper(int(k[i]));
 }
 m[i] = '\0';
 return m;
}



But what I don't understand is why in line 25 this produces an error, why can't we use k again on the left-hand side:

k[i] = toupper(int(k[i]))
It crashes if you define it as:

1
2
3
4
5
6
7
8
9
10
11
12
char* cct(char k[])
{
 //char *m = new char[10];

 int i = 0;
 for(i = 0; i < strlen(k); i++)
 {
  k[i] = toupper(int(k[i]));
 }
 k[i] = '\0';
 return k;
}


This crashes, that's why I need m
> trying to return local variable value back from function!
Nothing wrong with that. The problem is to return an address/reference to a local variable.

@FastLearner:const char* bpl[] = {"cAr","Pen", "hOmE", "coMputeR"};
And use delete[] if you allocated with new[]

@OP:
> i don't want to use string.h library.
You are using it, for `strlen()'
Also, main must return int
Thanks ne555. I had forgotten that I cannot assign a new value to a constant. Since it is an array of string literals, it produces an error. Also thanks for noting my mistake with new[] and delete[]
Topic archived. No new replies allowed.