I've got a structure which holds names and ages.
I've made a linked-list of these structures, using this as a pointer:
aNode *rootA;
in my main.
Now i send **rootA to a function like so
addElement(5,"Drew",&rootA);
Because i need to pass rootA by reference so that I can edit it in other functions (in my actual program i have two roots, so return will not work)
The problem is, in my program, i can't say access the structure members.
*rootA->age = 4;
for example doesnt work.
Hopefully you guys can help me out.
Thanks!
It's hard to tell from your question but it looks like the type of rootA in the last sample is aNode**. If so the reason why it's failing is that -> has higher precedence than *. You need to use a paren to correct this problem
(*rootA)->age = 4;
See full C Operator Precedence Table.
If the type of rootA is instead aNode*. Then you don't need to dereference in addition to using ->. Instead just use -> directly
rootA->age = 4;
I suspect you need to pass a pointer to the rootA variable, and not dereference it twice.
addElement(5,"Drew",&rootA);
Related
I have a relevant question that highlights my weak understanding of how to work with pointers. I declare a pointer variable called
FILE *MEMORY_CARD = fopen("card.raw", "r")
In my understanding, I have just declared a pointer called MEMORY_CARD that contains the information from a file called card.raw. Ok, now I would like to check if this pointer is NULL.
Do I do it like this:
if (MEMORY_CARD == NULL)
{
exit(1);
}
Or like this:
if (*MEMORY_CARD == NULL)
{
exit(1);
}
It seems like the first case is correct because I have seen it done this way, but based on my current understanding, I thought you would need to do it as per the bottom way. This is because I thought that every time you want to check a value at the address that the pointer is pointing to, you need to use the dereference operator such that *MEMORY_CARD is a reference to the information at that location which is what you want to check for null. I would think that the top way would be invalid as it is asking "Check if what is inside the variable MEMORY_CARD is NULL (Which I never declared. I declared a pointer called *MEMORY_CARD) Variable would be something like int i = 1, where I do not use a pointer.
I get the same confusion by the way when I need to check if what I declare with malloc is Null. Do I reference it with * prefixing the name, or just reference it like I would with a variable. Perhaps I am still confused to the basic definition of pointer vs variable. Hopefully these examples shed a light into a bigger picture concept that I am a bit foggy. I've looked at a few resources myself, but they do not seem to deep dive enough into minutia of this. Hopefully someone can help me clear this up. Thanks in advance!
MEMORY_CARD is the address of your opened file in memory
*MEMORY_CARD is the data of your file structure itself
so you should check MEMORY_CARD
This is because I thought that every time you want to check a value at the address that the pointer is pointing to, you need to use the dereference operator
This is true but does not apply. There is no need to to "check a value at the address", just the address.
The proper check below tests the pointer.
if (MEMORY_CARD == NULL)
{
exit(1);
}
I have the following nested struct:
hole 1x200 struct, diam 1x12 struct, which has the following fields: pos, freq1, fre12
That is:
hole(1 to 200).diam(1 to 12).pos
.freq1
.freq2
From a value (freq1 and freq2), I would like to obtain the field name of the struct. So I will need to find the value that matches with freq1 and freq2 and show the fieldname.
I tried to use structfun in order to apply a function to each field.
[struct.field]=structfun(#(x) find(x.freq1==27.059783995484867 & freq2==76.468355874897000))
But I guess I am writing something wrong on the code.
Also I create an anonymous fuction but I have the following error:
'Error using structfun / Inputs to STRUCTFUN must be scalar
structures'
. How ever when I verified if an specific value of the struct is scalar, I have a positive answerd: isscalar(hole(130).diam(10))
I belive I more near the solution using this script:
myfun=#(yourarray,desiredvalue) yourarray==desiredvalue;
%//Apply function to each field of scalar structure, it SCALAR??
desiredfieldindex=myfun(structfun(#(x) x,hole),26.697046257785030)
desiredFieldName=fNames(desiredFieldIndex)
I don´t know if I am in the rigth path, or I should utilize the function find. ALso I that case I don´t know how to implement it.
Couple of things.
FLOATING POINT VALUES! Careful!! Never compare a floating point value as val==0.3! do abs(val-0.3)<10^-8 or something similar. Read more here.
You are using structfun wrong. The function needs 2 arguments, and you are just passing 1! However, structfun will apply a function to each field so you are not using it rigth either in that sense. Lets see an example
example:
a.hithere=5;
a.notthis=3;
fun=#(x)x==5;
[isfive]=structfun(fun,a)
isfive =
1
0
As you can see, it applies the function to each of them. If you try to change the function to fun=#(x)x.hithere==5 you will get an error! As the function is applied to each field, and x.hithere.hithere or x.notthis.hithere do not exist.
If you want to check both values, you will need to check them independently making two separated calls to structfun or avoiding structfun and just making a for loop.
I started learning C in class and have started the assignment but I'm writing it one piece at a time. Currently I'm stuck because I may be doing something completely wrong but I don't know what I'm doing.
The error is pretty clear... The void type isn't something you can return / store in a variable. Instead it's a special type that means "nothing". You can't store nothing, you must ignore the return or return something.
displayCard only displays the cards, it does nothing to define them, so you shouldn't be trying to use it to create your cards. Instead, think about what a "card" actually is in your program--what, for example, do you have to pass to displayCard in order for it to know which card to display?
I'm changing some codes in a database library. The way it works I send a void pointer, to get the size of it I call a query and using the query I calculate the size of the structure. Now the problem is I receive the struct as params but the function fails before/in the middle of the first fetch. After that I need to clear the structure, but I dont even have the size.
I know the best way is send the size of the structure as a param, but I have thousands and thousands programs already compiled, the library is from 1996, so I need to find a way to calculate the structure size even if the type is void.
One idea I had was to calculate the position of the next element that is not in the structure
0x000010 0x000042
[int|char[30]|int|int][int]
So the size is 32, because the 0x00042-0x000010 is 32.
Is there a way to know when I got out of the structure.
the prototype of the function is
int getData(char* fields, void* myStruct)
I need to find out the structure size.
Sorry if I missed some information, the code is HUGE and unfortunately I cannot post it here.
No, in general there's no way, given a void *, to figure out what you're after. The only thing you can do is compare it against NULL, which of course doesn't help here.
Note that there's nothing in the void * that even says it points at a struct, it could just as well be pointing into the middle of an array.
If you have some global means of recording the pointers before they're passed to getData(), you might be able to implement a look-up function that simply compares the pointer value against those previously recorded, but that's just using the pointer value as a key.
That's an issue I still don't understand.
Sometimes I have to write:
NSString* myVariable;
myVariable = #"Hey!";
Then, for example I define a Structure "DemoStruct" and get an Variable that uses it. Lets say I have a Structure that has x and y vars from type double.
I want to pass this var to a method which then manipulates my var, and I want that this manipulation has effect on the context from which I passed the var to that method. So I need a pointer, right.
I pass it to the method like that:
[someObject someMethod:&myVarThatUsesTheStruct]
that method now looks like that:
- (void)someMethod:(DemoStruct*)myVar {
(*myVar).x += 10;
}
Before the call, the component x of the struct was lets say 1000. Now, 10 is added and it is 1010 after the method call.
But I really really hardly dont get it why I have to use the Asterisk * for myVar in the Method, since I say already in the Method Header that myVar is a POINTER to a DemoStruct. I just pass with &myVarThatUsesTheStruct the memory address.
Can someone explain why this is like it is?
As you say, myVar is a pointer. As such, myVar.x is not correct: it would by a field of a pointer, which has no sense in C/Objective-C.
If you want to access to the variable pointed to by a pointer, you have to add the asterisk: myVar is a pointer, *myVar is the variable pointed to by myVar.
Moreover, in your case, you can use a special construct of C by writing myVar->x, which is strictly equivalent to (*myVar).x.
All of this is standard C, not specific to Objective-C.
About your first example, you don't have to put an asterisk because you change the value of the pointer, not the value of the variable: myVariable is a pointer to an object which at declaration time is assigned the nil value. The next instruction (myVariable = #"Hey!") is an assignment of pointer values: #"Hey!" is a pointer to a NSString object. The value of this pointer (not the value of the pointed constant) is assigned to myVariable, which then points to the object #"Hey!".
Yes, this is diffucult to follow at first time...
* is the dereference operator. All that *myVar means is "Get me the thing that the pointer myVar points to". You need this distinction because you need to tell the computer that you want to change the thing that myVar points to, not myVar itself.
taken from the learning objective c link via the developers portal for iphone devs:
Objective-C supports both strong and weak typing for variables
containing objects. Strongly typed variables include the class name in
the variable type declaration. Weakly typed variables use the type id
for the object instead. Weakly typed variables are used frequently for
things such as collection classes, where the exact type of the objects
in a collection may be unknown. If you are used to using strongly
typed languages, you might think that the use of weakly typed
variables would cause problems, but they actually provide tremendous
flexibility and allow for much greater dynamism in Objective-C
programs.
The following example shows strongly and weakly typed variable
declarations:
MyClass *myObject1; // Strong typing
id myObject2; // Weak typing
Notice the * in the first declaration. In Objective-C, object
references are pointers. If this doesn’t make complete sense to you,
don’t worry—you don’t have to be an expert with pointers to be able to
start programming with Objective-C. You just have to remember to put
the * in front of the variable names for strongly-typed object
declarations. The id type implies a pointer.