Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > "basic" pointer question

Reply
Thread Tools

"basic" pointer question

 
 
Jonathan Bartlett
Guest
Posts: n/a
 
      06-27-2005
> My questions are these:
> 1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
> as well
> as their pointers?


I think you are confusing issues. The malloc() statements in your post
ARE allocating the space for the doubles.

last_values *lst_ptr = malloc(sizeof(*lst_ptr));

[however, this should actually be size(last_values)]

allocates memory for the entire last_values structure, which _includes_
your three pointers. However, they aren't pointing _to_ anything, yet.

When you do:

lst_ptr->lst_t = malloc(sizeof(double));
lst_ptr->lst_vc = malloc(sizeof(double));
lst_ptr->lst_ic = malloc(sizeof(double));

These are allocating space for the _actual double values_, and assigning
the pointers to theses values to the various members of lst_ptr.

However, I think, given your goal, you are going about this entirely the
wrong way. I do not know why you would even need a structure of
pointers for this, as opposed to just a structure of doubles.

> 2) Where and how should I define my structure last values assuming
> that I
> have no ability to change the related *_h file. i.e. can I just
> do it at
> the start of the function I'm creating everything in of do I have
> to do
> it at a higher level?


I have no idea what you are trying to do here. What function are you
trying to call? What "h" files are you trying to use? It looks like
some sort of generic callback-with-data interface. Anyway, it would at
least appear that you are way overusing pointers. Can you make it work
with just:

typedef struct {
double lst_t, lst_vc, lst_ic;
} last_values;

That seems to be a much easier plan. You can rid yourself of 3 malloc's
(and free's!) that way.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
 
 
 
steve
Guest
Posts: n/a
 
      06-27-2005
I'm trying to create a structure of three pointers to doubles. For
which I have:

typedef struct {
double *lst_t, *lst_vc, *lst_ic;
} last_values;

I then need to allocate space for last_values as well as assign the
value of a pointer to the assigned space. Which I think I'm doing by
using:

last_values *lst_ptr = malloc(sizeof(*lst_ptr));

I then want to associate the pointer *lst_ptr to an existing void
pointer in another structure that is defined as:

typedef struct _UserInstDef UserInstDef;
struct _UserInstDef {
char *tag;
void current_mask;
void *seniorData;
};

I do this by using:

UserInstDef->seniorData = lst_ptr;

And then I finally allocate space for all of the pointers to doubles
as:

lst_ptr->lst_t = malloc(sizeof(double));
lst_ptr->lst_vc = malloc(sizeof(double));
lst_ptr->lst_ic = malloc(sizeof(double));

The goal behind all of this is to be allocating space for 3 double
values and their pointers and then link those values to an existing
pionter so I can "smuggle" the values out of a function where I've just
created them.


My questions are these:
1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
as well
as their pointers?
2) Where and how should I define my structure last values assuming
that I
have no ability to change the related *_h file. i.e. can I just
do it at
the start of the function I'm creating everything in of do I have
to do
it at a higher level?
3) Is there something wrong with the line:
last_values *lst_ptr = malloc(sizeof(*lst_ptr));
Am I missing something fundamental here?

I'm pretty new to C so any help is appreciated.

Thanks for your time,

Steve

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-27-2005
"steve" <(E-Mail Removed)> wrote:

> I'm trying to create a structure of three pointers to doubles. For
> which I have:
>
> typedef struct {
> double *lst_t, *lst_vc, *lst_ic;
> } last_values;
>
> I then need to allocate space for last_values as well as assign the
> value of a pointer to the assigned space. Which I think I'm doing by
> using:
>
> last_values *lst_ptr = malloc(sizeof(*lst_ptr));
>
> I then want to associate the pointer *lst_ptr to an existing void
> pointer in another structure that is defined as:
>
> typedef struct _UserInstDef UserInstDef;
> struct _UserInstDef {
> char *tag;
> void current_mask;
> void *seniorData;
> };
>
> I do this by using:
>
> UserInstDef->seniorData = lst_ptr;
>
> And then I finally allocate space for all of the pointers to doubles
> as:
>
> lst_ptr->lst_t = malloc(sizeof(double));
> lst_ptr->lst_vc = malloc(sizeof(double));
> lst_ptr->lst_ic = malloc(sizeof(double));
>
> The goal behind all of this is to be allocating space for 3 double
> values and their pointers and then link those values to an existing
> pionter so I can "smuggle" the values out of a function where I've just
> created them.
>
>
> My questions are these:
> 1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
> as well as their pointers?


Yes, or find some other way to point them at some valid address - for
example, by assigning the address of a "normal" double to them.

> 2) Where and how should I define my structure last values assuming
> that I have no ability to change the related *_h file. i.e. can I just
> do it at the start of the function I'm creating everything in of do I have
> to do it at a higher level?


That depends mostly on the structure of the program you're writing.

> 3) Is there something wrong with the line:
> last_values *lst_ptr = malloc(sizeof(*lst_ptr));
> Am I missing something fundamental here?


As long as you have #include <stdlib.h>, nothing.

Richard
 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      06-27-2005
For my second question there, is declaring the structure in the
procedure where I "attach" the structure to the seniorData pointer ok.
I seem to recall something to do with the structure losing scope when
the procedure is exited. If that is the case can I just define the
structure at the top of the *.c file?

Sorry for the level of the questions but it's been years since I've
used c and even then it was c++.

Thanks again for the help.

 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      06-27-2005
Now that you mention it, using three doubles would work just fine. Not
sure how I came up with the idea for using the pointers.

So I would create the structure of three doubles and then make
seniorData point to the new structure. That does seem much easier.

As for the definition of the structure. The hearder file corresponding
to my c file is automatically generated by the software using my code
(long story). i.e. I'm writing file.c and file_h.c I can't change (I
think calling it the header file is the right term but I could be way
off). Basically what I'm asking is if I create the structure in a
function, say pre_analysis, and attach the structure to the one
variable that is passed to the function that I can mess around with
will the structure still be there for other functions to use the data
through userInst->seniorData->lst_* (* in the wildcard sense, not
pointer sense)?

Thanks again for the help

 
Reply With Quote
 
Netocrat
Guest
Posts: n/a
 
      06-27-2005
On Mon, 27 Jun 2005 14:26:48 +0000, Richard Bos wrote:

> "steve" <(E-Mail Removed)> wrote:
>
>> I'm trying to create a structure of three pointers to doubles. For
>> which I have:
>>
>> typedef struct {
>> double *lst_t, *lst_vc, *lst_ic;
>> } last_values;
>>
>> I then need to allocate space for last_values as well as assign the
>> value of a pointer to the assigned space. Which I think I'm doing by
>> using:
>>
>> last_values *lst_ptr = malloc(sizeof(*lst_ptr));
>>
>> I then want to associate the pointer *lst_ptr to an existing void
>> pointer in another structure that is defined as:
>>
>> typedef struct _UserInstDef UserInstDef; struct _UserInstDef {
>> char *tag;
>> void current_mask;
>> void *seniorData;
>> };
>>
>> I do this by using:
>>
>> UserInstDef->seniorData = lst_ptr;


What you've written isn't syntactically valid since UserInstDef is a type,
not a variable. I'm assuming that you're using UserInstDef here to refer
to a variable of type UserInstDef *, in which case what you've written
makes sense. If your variable truly is of type UserInstDef, you'd need to
replace the '->' operator with '.' as in:

variableOfTypeUserInstDef.seniorData = lst_ptr;

>> And then I finally allocate space for all of the pointers to doubles
>> as:
>>
>> lst_ptr->lst_t = malloc(sizeof(double)); lst_ptr->lst_vc =
>> malloc(sizeof(double)); lst_ptr->lst_ic = malloc(sizeof(double));
>>
>> The goal behind all of this is to be allocating space for 3 double
>> values and their pointers and then link those values to an existing
>> pionter so I can "smuggle" the values out of a function where I've just
>> created them.


You may have a valid reason to use pointers to doubles rather than plain
doubles, but if so you haven't explained it. Is there any reason why you
can't instead write:

typedef struct {
double lst_t, lst_vc, lst_ic;
} last_values;

And replace your references to lst_ptr->lst_t with &lst_ptr->lst_t;
similarly for lst_vc and lst_ic.

>> My questions are these:
>> 1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
>> as well as their pointers?

>
> Yes, or find some other way to point them at some valid address - for
> example, by assigning the address of a "normal" double to them.


If you use the code I gave above, then there is no need for any
allocations apart from:

last_values *lst_ptr = malloc(sizeof(*lst_ptr));

>> 2) Where and how should I define my structure last values assuming
>> that I have no ability to change the related *_h file. i.e. can I just
>> do it at the start of the function I'm creating everything in of do I
>> have to do it at a higher level?


I don't see any point in only doing it at the start of the function in
which you're creating everything since then it will only have scope in
that function. You won't be able to use variables of that type anywhere
else. Surely you will need to use it after you have assigned it...

> That depends mostly on the structure of the program you're writing.
>
>> 3) Is there something wrong with the line:
>> last_values *lst_ptr = malloc(sizeof(*lst_ptr));
>> Am I missing something fundamental here?

>
> As long as you have #include <stdlib.h>, nothing.


And remember to check whether malloc returned NULL.

Also to quote your reply post:

> For my second question there, is declaring the structure in the procedure
> where I "attach" the structure to the seniorData pointer ok. I seem to
> recall something to do with the structure losing scope when the procedure
> is exited. If that is the case can I just define the structure at the
> top of the *.c file?


You need to declare the structure so that it is in scope any time you want
to access a variable of that type (or a pointer to that type). You are
correct - as I have already explained it does go out of scope when you
exit that function.

So it sounds OK to just define it at the top of the *.c file if that's the
only place you will actually use it.

In other words, any files where you use UserInstDef but don't access its
seniorData member as struct *last_values - and don't in any other way
use a variable of type struct last_values or struct *last_values - don't
require struct last_values to be in scope. This applies even if you have
previously assigned a variable of type struct *last_values to the
seniorData member - the assignment will remain valid even though you can
only access it as void * and not struct *last_values.

 
Reply With Quote
 
steve
Guest
Posts: n/a
 
      06-27-2005
I wasn't aware of the "->" vs "." difference, that's solved a few
errors right there.

As for the double vs. pointer purely an oversight due to lack of
experience. I think I orignally thought pointers would have a sort of
global scope. Then I realized that made no sense and never realized
that using the pointers instead of just doubles makes just about as
much sense.

Right now I've got the declaration of the struct at the top of the c
file and that's the only place it's used so my scope should now be
fine.

I haven't forgotten about the NULL checks, but thanks for the reminder.

All in all the code should pretty much be polished thanks to all your
help.

Cheers and thanks again,

Steve

 
Reply With Quote
 
Jonathan Bartlett
Guest
Posts: n/a
 
      06-27-2005
Yes!

For more information on this kind of programming, you might take a look
at my DeveloperWorks article:

http://www-128.ibm.com/developerwork...-highfunc.html

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      06-27-2005
steve wrote:
>

.... snip overly complex solution effort ...
>
> The goal behind all of this is to be allocating space for 3 double
> values and their pointers and then link those values to an existing
> pionter so I can "smuggle" the values out of a function where I've
> just created them.


See what this does for you:

/* just define a type and give it a name */
/* a struct label lives in a different namespace */
/* all the typedef does is avoid retyping struct elsewhere */
typedef struct smuggled {
double v1, v2, v3;
} smuggled;

smuggled smuggler(/* params as needed */)
{
smuggled localvalue;

/* gyrations */
localvalue.v1 = firstvalue;
localvalue.v2 = secondvalue;
localvalue.v3 = thirdvalue;
return localvalue;
}

void smuguser(....)
{
smuggled data;

....
data = smuggler(...);
fprintf("The product is %f\n", data.v1 * data.v2 * data.v3);
....
}

many would prefer to replace the word 'smuggled' with "struct
smuggled" everywhere except in the original typedef, which would
become a simple struct definition:

struct smuggled {
double v1, v2, v3;
};

No gyrations with malloc, pointers, etc. are needed. KISS.

BTW, don't regret having given your solution. We respect people
who make an effort. The above assumes that I understand your
objective properly.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Rajan
Guest
Posts: n/a
 
      06-28-2005
1) Do I have to allocate space for the doubles lst_t, lst_vc, lst_ic
as well
as their pointers?
I think you need not allocate memory for the structure here, you can
just create an instance by writing.
last_values l_val;
But you need to allocate memory for the doubles pointers for that
structure.
I find that you have not typecasted the doubles pointers while
allocating memory , this could give you warnings which can be
irritating.
As far as following is concerned, I feel that if you want to assign
this to void pointer you can do the following
Moreover UserInstDef is a structure name for which you have'nt created
an instance of this struct so you have to create UserInstDef
userInstance;
userInstance->seniorData = (void *)&l_val;

2) Where and how should I define my structure last values assuming
that I
have no ability to change the related *_h file. i.e. can I just
do it at
the start of the function I'm creating everything in of do I have

to do
it at a higher level?

Doing at a higher level is always safer because if you were to use that
struct instance in a function above the declaration of the structure
then you won't be able to compile it since the function above does'nt
know anything about this struct.


3) Is there something wrong with the line:
last_values *lst_ptr = malloc(sizeof(*lst_ptr));
Am I missing something fundamental here?

There is no reason why you should allocate in the first place. As I
have written at the top you can just create an instance last_values
l_val; which should suffice.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments