passing array element to function

I am trying to pass a pointer to an array element to a function and have the function change the value, but I'm getting a memory overwrite error when the program runs. The program sends a string to the function which is then suppose to change all of the letters to uppercase. Any help would be appreciated.

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
43
44
45
46
47
48
49
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;
using namespace System;

void toUpper(char* letter, int len){
	
	char lower_case[] = { "abcdefghijklmnopqrstuvwxyz" };
	char upper_case[] = { "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };
	int array_size = 1 * sizeof(lower_case);
	int count = 0;

	char* ptr = letter;

		while(count < len){
			
			if(*letter == ' ') {
				count++;
				letter++;
				continue;
			}
			
			for(int i = 0; i < array_size; i++){

				if(*letter == lower_case[i]) {
					*letter = upper_case[i];
					break;
				}
				else if(*letter == upper_case[i]){
						*letter = upper_case[i];
						break;
				}
			}
		letter++;
		count++;
		}
}
	

int main(array<System::String ^> ^args)
{
	char* stars[][80] = { "Robert Redford", "Chuck Norris", "Alec Baldwin" };
	int count = 0;
	int len = strlen(*stars[0]);
	toUpper(*stars[0], len);
	cout << *stars[0] << endl;
}
I dont see the point of line 15. You dont use it anywhere in your function.

Ok. It seems you have a decent amount of errors. If im wrong someone correct me please. Ill start in the function.
It seems to me that you are passing in a char array and swaping the case of each letter in the array. so I will go on that assumption.

On line 12 I would change the code to
const int size = 26; //but that is just me.
You can get rid of line 15 its not used.

Line 19 should be
1
2
3
4
if(letter[count] == ' ')
{
//... I would get rid of letter++ i dont think that code does anything.
//i think when you do pointer++ it goes to next position but that is what were using the count for 

this is what the for statement should be
1
2
3
4
5
6
7
8
9
10
11
12
for(int i = 0; i < array_size; i++){

				if(letter[count] == lower_case[i]) {
					letter[count] = upper_case[i];
					break;
				}
				else if(letter[count] == upper_case[i]){
						letter[count] = upper_case[i];
						break;
				}
			}

then for the end of the while statement get rid of the
letter++ on line 36.

Line 44 might give you problems because you are making a 2d array I think wich you do not need. I think you wrote a function for a single dimension array. If I were you I would change it too
char* stars[] = {"Robert Redford", "Chuck Norris", "Alec Baldwin" };
line 45 you never use the count so why make it. You can get rid of this line.
Line 47 should be
toUpper(stars, len);
line 48 im not sure if you are trying to print the first element or what.
if your tring to print the first element you can simply do the following
cout << stars[0] << endl;
if your trying to print the whole array
1
2
for(int i = 0; i < len; i++)
cout << stars[i];


let me know if this helped or if I made any mistakes.
I found the error. It is on line 44:

 
char* stars[][80] = { "Robert Redford", "Chuck Norris", "Alec Baldwin" };


should be this:
 
char stars[][80] = { "Robert Redford", "Chuck Norris", "Alec Baldwin" };


line 15 is not needed. I was using it for testing purposes when it wouldn't work.

Lines 19-23 are needed:
1
2
3
4
5
			if(*letter == ' ') {
				count++;
				letter++;
				continue;
			}


but you're right it works this way as well:
1
2
3
4
5
6
		while(count < len){
			
			if(letter[count] == ' ') {
				count++;
				continue;
			}


this code section looks for a blank space. If there is one, then there is no need to enter the for loop and look for a letter, so it's bypassed. I use letter++ rather than the other notation because that's how it was taught to me. I tried it your way and it works as well - thanks for your help.
great!!! Yea after I posted I remeber my teacher saying you could do ++ on a ptr. but we have always used count or something. glad it works.
Topic archived. No new replies allowed.