Hi, I'm trying to declare a pointers value inside of a function, and then manipulate that data the pointer points to. This is the test program I compiled and like the original it gives the error "undeclared identifier". At both occurrences of *somevar in the build function. Thank you for your time.

int main()
int len; char *somevar;
len = build(&somevar);

	int build(char **othervar)
	int tlen = 10;
	*somevar = (char*)malloc(tlen*sizeof(char));
	*somevar = {"something"};
7 Years
Discussion Span
Last Post by shaynerossum

As somevar is declared in main() you can't refer to it in build().
You are passing it into build() with the name othervar, so if you replace somevar by othervar in build() you will be able to compile.
Other points to note:
Given this forum, I'm assuming this code is C++, so use new instead of malloc.

*somevar = {"something"}; is going to set somevar with the value of a pointer to "something", thereby overwriting the pointer to memory you have previously allocated. I doubt this is what you had in mind. You probably want to copy "something" into the allocated memory, so you could do that with

strcpy(*somevar, "something");

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.