[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Strange C behaviour



Could anyone explain this to me?

Here's the program:

#include <stdio.h>

main()
{
	char ptr[10];
	
	printf("%p %p\n", ptr, &ptr);
}

And the output:

0xbffffbf0 0xbffffbf0

Or in other words, the '&' operator does not do anything. In particular that
means that the following program will dump core:

#include <stdio.h>
#include <stdarg.h>

void set(int s, ...)
{
	va_list args;
	void *p;	
	
	va_start(args, s);
	p = va_arg(args, void *);
	**((char **)p) = 'm';
	va_end(args);
}

void test1(ptr)
char ptr[];
{
	printf ("test1: ptr=%p %p\n", ptr, &ptr);
	set(1, &ptr);
	printf ("test1: ptr=%p\n", ptr);
}

main()
{
	char ptr[14];
	
	printf("main: ptr=%p\n", ptr);
	test1(ptr);
	set(2, &ptr);
	printf("main: ptr=%p\n", ptr);
}

So how do I call set() correctly so that it works from main() as well as from
test1()?

Michael
-- 
Michael Meskes                         | Go SF 49ers!
Th.-Heuss-Str. 61, D-41812 Erkelenz    | Go Rhein Fire!
Tel.: (+49) 2431/72651                 | Use Debian GNU/Linux!
Email: Michael@Fam-Meskes.De           | Use PostgreSQL!


Reply to: