Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Passing pointer to a function

$
0
0
I'm trying to pass a char pointer from main to a function, where the function will create a replica of the pointer(so as not the manipulate the original) passed to it and reverse it and return it.

Reversing isn't a problem, but trying to get the pointer to pass properly to the function is a problem.

Attempt:
#include <iostream>
#include <string>

using namespace std;

void f1(char* param_str, int size){
	
	//create a replica of pointer
	char* str = param_str;

	for(int i=0; i<size; i++){
		cout << (str[i]) << endl;
	}

}

int main(){
	
	char* str = new char[5];
	str[0] = 1;
	str[1] = 1;
	str[2] = 1;
	str[3] = 1;
	str[4] = 1;
	f1(str, 5);

	system("pause");
	return 0;
}



Output:
Posted Image

EDIT: Problem solved. I realized that i was setting ints to a char variable and that was why i was getting weird output.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>