Hello everybody,
I have spend the last 6 hours on trying to access a char * type within a static struct without success. I guess I use the pointers wrong but after trying "everything" I am pretty lost right now. So hopefuly someone of you can help me.

I have given a:

typedef char * msg;

and the following struct:

static struct queue{
  msg message;
  struct queue * next;
}myQueue;

I have a method which gives me a msg-type, all i want is to make this msg-type accessable by saving it into the struct which is static.

But codes like myQueue.message = "received message parameter" only seem to work within the scope of the function and not like a static variable should.


Any suggestions?

Lars

I'm really confused at how your using this static variable. Is it a local variable within a function? Maybe if you gave us an example of how you intend to use it..i.e. Post the code that's supposed to use the static variable.

I think I understand(I'm probably wrong but you gave so little information). This will return the address of the static structure so that you can access the msg member..Note this breaks the reason for having local static variables.

#include <stdio.h>

typedef char * msg;

void** myfunc()
{
	static struct queue
	{
		msg message;
		struct queue * next;
	}myQueue;

	fprintf(stdout, "msg->%s\n", myQueue.message);

	return (void**)&myQueue;
}

int main()
{
	void **vptr = myfunc();

	*vptr = (void*)"this is some string";

	myfunc();
	return 0;
}

Hey thanks for the answer. I am sorry that I could not describe my problem more precisely.

I have one function a() which gets a parameter of type msg. I want to save this parameter in the struct myQueue to make it accessible in another function b().

After assigning the parameter to the struct variable I can access the address &myQueue.message and the value myQueue.message in a().

In b() I can see the same address &myQueue.message but the value is empty.

typedef char * msg;

static struct queue{
  msg message;
  struct queue * next;
}myQueue;

// b() gets called after a()
b(){
 // This returns the same adress but an empty value 
  printf("%x, %s",&myQueue.message, myQueue.message);
}


int a(msg m){
  myQueue.message = m;
  
// This returns the expected value of m and the address 
  printf("%x, %s",&myQueue.message, myQueue.message);
}

Your defining your static structure as global...So why can't you access it? Try this

#include <stdio.h>

typedef char * msg;

static struct queue
{
  msg message;
  struct queue * next;
}myQueue;


void b()
{
  printf("b()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

void a(msg m)
{
  myQueue.message = m;
  printf("a()->%p, %s\n",(void*)myQueue.message, myQueue.message);
}

int main()
{
	a("this is the string");
	b();
	return 0;
}

Thanks, your help was very appreciated!

Your code did not help me much though, I guess I still had a problem to define the problem more precise as I did not now were the problem was. The code you gave me is working but not in my environment. Today I suddenly remembered the "strcpy()" function, and now it is working. :)

Thanks, your help was very appreciated!

Your code did not help me much though, I guess I still had a problem to define the problem more precise as I did not now were the problem was. The code you gave me is working but not in my environment. Today I suddenly remembered the "strcpy()" function, and now it is working. :)

OK, its not an access problem but a copying a c-string over problem. Remember to allocated memory for your structure pointer before you copy the c-string data over.

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.