struct manipulation

  • Thread starter Darek Adamkiewicz
  • Start date
D

Darek Adamkiewicz

Hello folks
I hope code below will explain what I'm trying to achive.
It seems trivial/common but not for me at the moment :(
Help very apreciated.
Regards
Darek.
-----------------
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

typedef struct Test {
char name[128];
int count;
};

int test(Test *t);

int test(Test *t)
{
Test *loct = t;
t = (Test *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Test));
strcpy(t->name,"TestEntry");
t->count = 256;
//? so address of t has been changed, is there a way to assign struct
values to old one?

return 0;
}

int main()
{
Test *t;
char out[128];
test(t);
// Is there a way to get values passed to t?
// now of course printf statement doesnt print proper string value
printf("%s",t->name);
return 0;
}
 
R

Richard Bos

Darek Adamkiewicz said:
#include <windows.h>

This is not an ISO C header, so it's off-topic here.
#include <stdio.h>
#include <tchar.h>
Ditto.

typedef struct Test {
char name[128];
int count;
};

int test(Test *t);

int test(Test *t)
{
Test *loct = t;
t = (Test *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Test));

Neither HeapAlloc() not GetProcessHeap is an ISO C function, so they're
off-topic here.
Even so, you probably want

t = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof *t);

since casts are rarely necessary for properly declared allocation
functions in C, and sizeof *pointer_i'm_allocating_for means less
maintenance than sizeof (some_type) in case the type of the pointer
changes.
strcpy(t->name,"TestEntry");
t->count = 256;
//? so address of t has been changed, is there a way to assign struct
values to old one?

Sure. You've taken the precaution of saving the old value of t in loct;
since you didn't change it when you changed t, it still points at the
old address.

Note, however, that you never pass the new value of t outside of this
function, and don't free the allocated memory, either; this means that
you have, in all probability, created a memory leak. (I say in all
probability, because, HeapAlloc() not being ISO C, there is no saying
what it might be doing behind the screens - including, for example,
keeping a global record of HeapAlloc()ated pointers. Had you use
malloc(), I'd have been sure that this _is_ a memory leak.)
Note that a pointer is just an object, not something magical: passing
one to a function and then changing its value inside that function will
not change the value in the caller any more than doing the same thing to
an int will. See the FAQ: said:
int main()
{
Test *t;
char out[128];
test(t);
// Is there a way to get values passed to t?

t is an object; one doesn't "pass" values to objects. One passes values
to functions, and assigns them to objects. But see the FAQ entry above.

Richard
 
D

Dan Pop

In said:
I hope code below will explain what I'm trying to achive.

I'm afraid you're overoptimistic. The code below doesn't make any sense.
#include <windows.h>

Undefined behaviour.
#include <stdio.h>
#include <tchar.h>

Undefined behaviour.
typedef struct Test {
char name[128];
int count;
};

int test(Test *t);

Pointless declaration.
int test(Test *t)

Why make test() return int, if it doesn't return anything useful to its
caller?
{
Test *loct = t;
t = (Test *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(Test));

There is no such function in the standard C library. When posting to this
so that we can figure out said:
strcpy(t->name,"TestEntry");

Undefined behaviour: strcpy is not a function returning int and you have
provided no declaration for it. Include said:
t->count = 256;
//? so address of t has been changed, is there a way to assign struct
values to old one?

Nope, the address of t has not changed and it cannot change. If you're
talking about the *value* of t, you have saved the original value in
loct and you can use it.
return 0;
}

Note that the new value assigned to t in test() gets lost at this point.
The object allocated in test() becomes a memory leak, because its address
is lost forever.
int main()
{
Test *t;
char out[128];
test(t);

You have NOT initialised t. What's the point of passing an indeterminate
value to the test() function. If you don't understand what I mean, read
the FAQ asap.
// Is there a way to get values passed to t?

Yes, if t is initialised, test() can store something into the object it
points to. But your test() doesn't even try...
// now of course printf statement doesnt print proper string value

Obviously, because:

1. t was passed to test() uninitialised.

2. test() saved the original value of t, but didn't attempt to do anything
else with it.
printf("%s",t->name);
return 0;
}

You really need to read the chapter dealing with functions in your
favourite C book. You don't seem to understand how function argument
passing works in C. And the way you handle t in main() denotes a lack of
basic understanding about pointer usage, too.

At your level, it makes *absolutely* NO sense to mess with Windows
programming. Restrict yourself to the basic features of the C language.

Dan
 
D

Darek Adamkiewicz

Richard said:
So while I can do the following (is it correct?)
--------------------------
#include <stdio.h>

void f(int *ip);

void f(int *ip)
{
static int dummy = 5;
*ip = dummy;
}

int main()
{
int *ip;
f(ip);
printf("%d\n", *ip);
}
--------------------
I can't do similar with struct in C? - pass struct pointer to function,
declare local struct, allocate memory, set values to and finally make
that pointer address contain pointer to that struct?

Regards
Darek
 
D

Darek Adamkiewicz

You'll be surprised but I read K&R book and wrote some working till
today C ANSI programs about 3 years ago - It's seems I forgot almost
everything I learned :( or overrode by perl data manipulation techniques
which allow programmer to do (almost) everything he/she wants. Thanks
anyway - I must do "my homework" again :(

Regards
Darek
 
R

Richard Bos

Darek Adamkiewicz said:
So while I can do the following (is it correct?)
--------------------------
#include <stdio.h>

void f(int *ip);

void f(int *ip)
{
static int dummy = 5;
*ip = dummy;
}

int main()
{
int *ip;
f(ip);
printf("%d\n", *ip);
}

That's almost correct. Point ip at a valid int before you pass it to
f(), then it's correct.
I can't do similar with struct in C?

Wrong. This

#include <stdio.h>

struct st {
int i;
};

void f(struct st *stp)
{
struct st dummy={5}
*stp=dummy;
}

int main()
{
struct st temp;
f(&temp);
printf("%d\n", temp.i);
}

is perfectly legal.

However, it isn't what you were doing in the original post. There, the
object you modified in f() was a pointer. Not the struct the pointer
pointed to - the pointer itself.
As the FAQ explains, if you wish to change _anything_ in a function, you
must pass a pointer to it. So if the object you want to change is a
pointer, you must pass a pointer _to_ that pointer. Like this:

#include <stdio.h>

struct st {
int i;
};

void f(struct st **stp)
{
struct st dummy={5}

*stp=malloc(sizeof **stp);
if (*stp)
**stp=dummy;
}

int main()
{
struct st *stp_pointer;
f(&stp_pointer);

if (stp_pointer)
printf("%d\n", stp_pointer->i);
else
puts("Null pointer - allocation failure.");

free(stp_pointer);
}

Richard
 

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

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top