Re: Seeting malloc pointer to NULL [2] -Totally confused!!!!!

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Sat, 11 Oct 2008 12:28:00 -0700, Robby
<Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hello,

Hope you guys are still there! ...... Need help.

It worked, then it didn't work, then it worked and now it doesn't work.

I don't know exactly how to integrate pointers to pointers when it comes to
using them with functions. I am getting weird results again! I will let you
guys take a look at the last code that I ahve been using! Please get back!

The only weird result you should get is a failure to compile. At that
point, any attempt at execution is a waste of time.


============================================
// #includes here!

The last set of includes I saw was half C and half C++. Maybe you
should show the ones you are using now.


enum enum_OC{e_CREATE=1, e_MODIFY, e_FREE};

typedef struct passCode {
int TOUCHES;
} PASSCODE;

void config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES );

void main()
{
PASSCODE *obj_PASSCODE; // Create a PASSCODE type pointer

config(e_CREATE, obj_PASSCODE, 4);

The second argument is incompatible with the function prototype. This
was a bad idea that has been implemented incorrectly.


//... other code..... functions using obj_PASSCODE!

config(e_FREE, obj_PASSCODE, ...); //Free the Pointer !

}

config( enum_OC OC, PASSCODE **obj_PASSCODE, int TOUCHES )
{
switch (OC)
{
//Now that I am passing obj_PASSCODE with multi indirection, how do

No you are not. That is what this function wants to receive but it is
not what you sent.

//I dereference it in this function? Is the code below right? (I don't no
anymore!)

case e_CREATE:
// **obj_PASSCODE = NULL ???

Think about it. Within this function, obj_PASSCODE is a struct**.
Therefore, *obj_PASSCODE is a struct* and **obj_PASSCODE is a struct.
Do you think it makes sense to say set struct to NULL?

obj_PASSCODE = NULL;
obj_PASSCODE = malloc (sizeof (PASSCODE));

Again, obj_PASSCODE is a struct**. It must point to a struct*. You
do not allocate space for a struct*. You allocate space for a struct.


case e_MODIFY:
// **obj_PASSCODE->TOUCHES = TOUCHES; ???

What is the relative precedence of * and ->? You statement will be
parsed as
**(obj_PASSCODE->TOUCHES) = TOUCHES;

How many was can this be wrong. The only type that can appear on the
left of the -> operator is struct*. obj_PASSCODE is not an expression
of this type. Even if it were, ->TOUCHES is an int, not a pointer.
You cannot apply any dereference operators to non-pointer types.

obj_PASSCODE->TOUCHES = TOUCHES;

Again, obj_PASSCODE is not a struct* and cannot appear on the left of
the -> operator. I told you this was a bad idea.

//....

break;

case e_FREE:
free(obj_PASSCODE);
obj_PASSCODE = NULL;
break;
}
}
=================================================

What's happening is that when I return to main and pass obj_PASSCODE to
another function, the data I set in config seems to be lost?

At no point in this code does your function ever set the value of the
object obj_PASSCODE in main. If you would stop using the same name
for different objects, some of your confusion might be eliminated.



Passing a variable by value:

f1( int x);
main{
int x;
f1(x);
}
f1( int x);
{...}

Passing a variable by reference:

This is not a pass by reference.

f1( int *i)
main{
int x;
f1(&x);

You new to use the & operator here. Why didn't you use it in "your
real code".

}

f1( int *i)
{...}

Am I not passing a pointer by reference using multi indirection? like this:

No you are not passing by reference in C, ever.

=========================================
void config( PASSCODE **obj_PASSCODE);

void main()
{
PASSCODE *obj_PASSCODE;
config( obj_PASSCODE);

This is the same constraint violation mentioned above. The type of
your argument is incompatible with the type expected by the function.

}

config( PASSCODE **obj_PASSCODE)
{...}
===========================================

The changes that occur in the function should be remembered after I get out
of the function.

Changes made to objects local to a function are NEVER remembered when
you leave the function. If you code the function properly and call it
properly, config could make changes to objects defined in main.

1 - For any object in main you want to change in config, pass the
address of that object as a pointer.

2 - In order for config to make the change to that object, it must
dereference the pointer it received.


Help sincerely appreciated!

Go back to the original approach and have config return the new value.

--
Remove del for email
.



Relevant Pages

  • Re: why cannot assign to function call
    ... and passing the array by reference? ... void mysub (int k, int n) { ... say C was definitely pass-by-reference (for arrays)? ... If one could only use v as a pointer (i.e. access ...
    (comp.lang.python)
  • Re: Confusion of Malloc and Sizeof
    ... without passing the number or having a global variable. ... Another approach is to have a structure containing a pointer to the ... int main (int argc, char **argv) ...
    (comp.lang.c.moderated)
  • Re: Trying to understand pointers for function paramaters
    ... > int *p ... expecting an int but you are passing an int*. ... the first two are to what you wrote when not calling a function. ... pointer and evaluates to the object being pointed to. ...
    (comp.lang.c)
  • Re: Passing pointers?
    ... I am wondering when passing a pointer to a function and require to further ... void x ... int z=99; ...
    (microsoft.public.vc.language)
  • Seeting malloc pointer to NULL [2] -Totally confused!!!!!
    ... typedef struct passCode { ... int TOUCHES; ... Passing a variable by value: ... config(obj_PASSCODE); ...
    (microsoft.public.vc.language)