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"};
	}

Recommended Answers

All 2 Replies

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");

Thank you my program worked as intended after corrections.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.