Question about pointers

S

silusilusilu

Hi,
i have some troubles with a code like this:

void main (void)
{
char array[2];
function1(array);
}

void function1(char * data)
{
....
function2(data)
}

function2(char * func2data)
{
...
}

I know that function2 works well, but in this case array memory address seems lost: the array memory address before funtion1 is different from its address after function1
What's wrong?
 
J

John Gordon

In said:
Hi,
i have some troubles with a code like this:
void main (void)
{
char array[2];
function1(array);
}
void function1(char * data)
{
....
function2(data)
}
function2(char * func2data)
{
...
}
I know that function2 works well, but in this case array memory address
seems lost: the array memory address before funtion1 is different from its
address after function1
What's wrong?

Show us the code you're using to display the array addresses.

I'll make a guess that within each function you're displaying the address
of the local pointer itself, instead of what it points to.
 
J

James Kuyper

Hi,
i have some troubles with a code like this:

void main (void)

The standard specifies two ways of defining main(); you can use any
other method that is equivalent to one of the two standard ways. Both
methods return an 'int'. Declaring 'int' to return anything other than
'int' means that a C compiler is not required to accept your code.
{
char array[2];
function1(array);

At this point, there's no declaration in scope for function1(). In C90,
it would be implicitly declared as returning 'int' and taking a fixed
but unspecified number of parameter of unspecified types; this was a bad
idea, because it's very error prone, which is why it's no longer allowed
in C99.
You should provide a function prototype. The simplest way, and the one I
prefer, is to define function1() before defining main() - the first part
of the function definition qualifies as a function prototype prototype.
Other people find it confusing to declare functions before calling them,
in which case just write a prototype that is not a definition:

void function(char *);
}

void function1(char * data)
{
....
function2(data)

There's a missing ';'.
}

function2(char * func2data)

You didn't declare a return type for function2, which is another
indication that you're writing for C90. In C99 and later, such
declarations are no longer allowed. I recommend providing a function
prototype.
{
...
}

I know that function2 works well, but in this case array memory address seems lost: the array memory address before funtion1 is different from its address after function1

I'm not sure why you think the address has changed; if you could provide
the evidence you used to reach that conclusion, we can probably explain
to you why that evidence doesn't mean what you thought it meant.

Here's a minor re-write of your code:

#include <stdio.h>
static void function2(char * func2data)
{
printf("funcdata:%p &funcdata:%p\n",
(void*)func2data, (void*)&func2data);
}

static void function1(char * data)
{
printf("data:%p &data:%p\n", (void*)data, (void*)&data);
function2(data);
}

int main (void)
{
char array[2];
function1(array);
printf("array:%p &array:%p\n", (void*)array, (void*)&array);
return 0;
}

Here's the results I got:
data:0xbf95577e &data:0xbf955760
funcdata:0xbf95577e &funcdata:0xbf955740
array:0xbf95577e &array:0xbf95577e

All of the pointers that should be equal printed out the same; all of
the pointers that should be different printed out differently, so that
output matches my understanding. If anything about it seems odd, let us
know, and someone will be able to explain it to you.
 
S

Seebs

Hi,
i have some troubles with a code like this:

Yes, yes you do.
void main (void)

Probably irrelevant, but this declaration is incorrect. main() returns
an int.
{
char array[2];
function1(array);
}
void function1(char * data)
{
....
function2(data)
}
function2(char * func2data)
{
...
}
I know that function2 works well,

And how do you know that?
but in this case array memory address seems lost:

And how do you know that?
the array memory address before funtion1 is different from its
address after function1

How are you determining this?
What's wrong?

What's wrong is you've provided insufficient information to give
any information about what happened, although I have a guess. I note
also that you are using different names in each function, so you may not
be aware that each function sees only its own argument list; you could
use the same names for these variables everywhere. (Although I won't in my
test case because I suspect it'll be easier to describe what's happening
with unique names for everything).

First, let's make a program that would actually illustrate this:

#include <stdio.h>

void func2(char *data2) {
printf("in func2: data2 is %p\n", (void *) data2);
}

void func1(char *data1) {
printf("in func1, pre: data1 is %p\n", (void *) data1);
func2(data1);
printf("in func1, post: data1 is %p\n", (void *) data1);
}

int main(void) {
char array[2];
printf("in main, pre: array is %p\n", (void *) array);
func1(array);
printf("in main, post: array is %p\n", (void *) array);
return 0;
}

My guess is, if you run this, you will find that all five lines of
output produce the same value. And the array's address doesn't change...

But!

It might be that the local value "data1" in func1 is being changed. And
if, in func2, you did something to the contents of data2, which overran
that array, on some systems that might even be fairly likely. Or things
might go horribly wrong in other ways. But since you didn't show any
actual working code, no one can tell.

-s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top