Why...? (Variable error)
Author |
Message |
Jerrik
|
Posted: Wed Apr 23, 2008 10:13 pm Post subject: Why...? (Variable error) |
|
|
code: | #include <stdio.h>
typedef struct {
int x, y, z;
char name[];
} testit;
void AlterIt(testit newMyVar){
newMyVar.x = 5;
}
int main(){
testit myvar;
myvar.x = 10;
printf ("Before: %d\n", myvar.x);
AlterIt(myvar);
printf ("After: %d\n", myvar.x);
return 0;
} |
In essence, why does it produce a 10 when I clearly changed the value to 5?
Its changing it locally not globally. It makes a copy of myvar onto newMyVar, so the changes are made to the new variable.
What is the *proper* method here? Do I use pointers? I think thats it. I'm just learning C, and *standards* seem hard to find.
Thanks for any assistance. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
CodeMonkey2000
|
Posted: Wed Apr 23, 2008 10:16 pm Post subject: RE:Why...? (Variable error) |
|
|
Because you are passing the variable in with call-by value. What this means is that the variable you pass is is recreated for that function. It is independent of the variable you passed. What you want is call-by reference. Here's how you do it:
code: | void AlterIt(testit &newMyVar){
newMyVar.x = 5;
} |
The & means "location at". |
|
|
|
|
|
wtd
|
Posted: Wed Apr 23, 2008 10:17 pm Post subject: RE:Why...? (Variable error) |
|
|
Because you didn't change the value of the "x" in "myvar". You changed the "x" in "newMyVar".
You need pointers. |
|
|
|
|
|
Jerrik
|
Posted: Wed Apr 23, 2008 10:26 pm Post subject: Re: RE:Why...? (Variable error) |
|
|
CodeMonkey2000 @ Wed Apr 23, 2008 10:16 pm wrote: Because you are passing the variable in with call-by value. What this means is that the variable you pass is is recreated for that function. It is independent of the variable you passed. What you want is call-by reference. Here's how you do it:
code: | void AlterIt(testit &newMyVar){
newMyVar.x = 5;
} |
The & means "location at".
error: expected ?;?, ?,? or ?)? before ?&? token
Quote: Because you didn't change the value of the "x" in "myvar". You changed the "x" in "newMyVar".
You need pointers.
I kept getting " error: request for member ?x? in something not a structure or union". So I kept thinking, and I realized I needed:
myvar->x = 10;
not
myvar.x = 10
Thank you for helping me, help myself, fix my brain problem.
EDIT:
Actually there is still a problem with my brian:
Quote: testit *myvar;
myvar->x = 10;
What am I still doing wrong? It compiles fine, but I get segfault. I figured I would stop hurting my computer and ask whats up. It seems right to me, I guess my brain still needs work.
EDIT:
I think it's because I've created a pointer to a structure, but C hasn't created an actual structure. Cool.
EDIT:
Look at me, my brain isn't so broken after all. Is there a way to make a function that creates a structure variable and returns the pointer, but doesn't erase it after the local scope is gone?
code: |
testit* Init(){
testit NewVar;
return &NewVar;
}
|
I tried using static, but of course that means NewVar is completely global, so it isn't recreated in the memory everytime the function is called. Any takers?
Additional EDIT:
The idea is that, rather then having:
type var;
type* varPointer;
type var2;
type* var2Pointer;
I could have
type* varPointer = Init();
type* var2Pointer = Init();
It doesn't have to be with the method provided above, but in some semblance of that. |
|
|
|
|
|
md
|
Posted: Thu Apr 24, 2008 12:03 am Post subject: RE:Why...? (Variable error) |
|
|
you should look into malloc() and free(). The former allocates memory for your pointer, the later frees it when you are done. |
|
|
|
|
|
wtd
|
Posted: Thu Apr 24, 2008 12:24 am Post subject: RE:Why...? (Variable error) |
|
|
Pointers when simply declared are either null or some random address. Dereferencing either is going to lead to a segfault. |
|
|
|
|
|
btiffin
|
Posted: Thu Apr 24, 2008 5:28 am Post subject: Re: Why...? (Variable error) |
|
|
Try
code: | alterit(testit *var) {
var->x = 5;
}
|
Just in case this is homework, I'll let you fill in the blank on how alterit should be called in your main.
or better yet, follow the advice of md and wtd and look into malloc and free. Your last post is over complicating things. Not that C isn't over complicated for a language with so few actual keywords and syntax rules.
Cheers |
|
|
|
|
|
Vermette
|
Posted: Thu Apr 24, 2008 9:32 am Post subject: Re: RE:Why...? (Variable error) |
|
|
md @ April 24th 2008, 00:03 wrote: you should look into malloc() and free(). The former allocates memory for your pointer, the later frees it when you are done.
This.
malloc() assigns memory on the heap. When you simply declare a variable/struct in a function it's being declared and allocated on the program stack. When said function returns your stack pointer rolls back and if you returned a pointer, it's now pointing to an 'free' location in memory. It won't take long for the data that used to occupy that spot to be overwritten. Since with malloc your data will be on the heap, it will persist after the function returns.
And this is the point when you're supposed to learn that every malloc() should have an associated free() to go with it. C doesn't support garbage collection (the reclamation of unreferenced yet allocated memory). If you malloc some space in a function, then return without calling explicity free()ing it or returning a pointer in some fashion, you've just introduced a memory leak! Do it enough times and you will be in trouble |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Jerrik
|
Posted: Thu Apr 24, 2008 12:21 pm Post subject: Re: Why...? (Variable error) |
|
|
Not for a school assignment, just for fun.
Quote: This.
malloc() assigns memory on the heap. When you simply declare a variable/struct in a function it's being declared and allocated on the program stack. When said function returns your stack pointer rolls back and if you returned a pointer, it's now pointing to an 'free' location in memory. It won't take long for the data that used to occupy that spot to be overwritten. Since with malloc your data will be on the heap, it will persist after the function returns.
And this is the point when you're supposed to learn that every malloc() should have an associated free() to go with it. C doesn't support garbage collection (the reclamation of unreferenced yet allocated memory). If you malloc some space in a function, then return without calling explicity free()ing it or returning a pointer in some fashion, you've just introduced a memory leak! Do it enough times and you will be in trouble Smile
code: | #include <stdio.h>
typedef struct {
int x, y, z;
} mytype;
int main(){
mytype* mypointer;
mypointer = malloc(sizeof(mytype));
mypointer->x = 10;
printf ("%d\n",mypointer->x);
free(mypointer);
return 0;
}
|
I like C =)
I thought malloc was mutex; so I was a little confused at first.
Thanks for all the help everyone, I appreciate it. |
|
|
|
|
|
md
|
Posted: Thu Apr 24, 2008 12:48 pm Post subject: RE:Why...? (Variable error) |
|
|
Just for reference, you can also use the dereferencing operator C: | int* int_ptr;
int_ptr = malloc(sizeof(int));
*int_ptr = 10;
... |
|
|
|
|
|
|
wtd
|
Posted: Thu Apr 24, 2008 1:51 pm Post subject: RE:Why...? (Variable error) |
|
|
How about:
code: | #include <stdio.h>
typedef struct {
int x, y, z;
} mytype;
mytype *new_mytype(int x)
{
mytype *result = malloc(sizeof(mytype));
result->x = x;
return result;
}
int main(){
mytype *mypointer = new_mytype(10);
printf("%d\n", mypointer->x);
free(mypointer);
return 0;
} |
|
|
|
|
|
|
|
|