CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Dec 2009
    Posts
    1

    problem getting function to change value of array element

    Newbie to C++ programming. 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.


    Code:
    #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;
    
    		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;
    }
    
    |
    Last edited by Marc G; December 15th, 2009 at 09:34 AM. Reason: added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured