473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

what is the type for this call?

Neo
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;

word_array *my_array;

what would be the data type of this:
myarray->word1
How come it is a uint32_t type and is giving the value of word1? isn't
it still a pointer which is not dereferenced yet?
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
Nov 6 '08 #1
11 3644
Neo wrote:
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;
(Why are you calling the struct a "str32" and the type "word_array"?
Neither name seems appropriate and their relationship is tenuous.)
word_array *my_array;

what would be the data type of this:
myarray->word1
unint32_t.
How come it is a uint32_t type and is giving the value of word1?
You declared `word1` as a unint32_t, so that's what it is.
isn't it still a pointer which is not dereferenced yet?
No.
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
`A->B` is `(*(A.B))`; the arrow has done the dereference for you.

--
"2008 is when it all changes, and you've got to be ready." Unsaid /Torchwood/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nov 6 '08 #2
Neo wrote:
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;

word_array *my_array;

what would be the data type of this:
myarray->word1
How come it is a uint32_t type and is giving the value of word1? isn't
it still a pointer which is not dereferenced yet?
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
To my understanding either u use (*my_array).word1 or my_array->word1.
Parenthesis are need for precedence.
Nov 6 '08 #3
Chris Dollin wrote:
`A->B` is `(*(A.B))`; the arrow has done the dereference for you.
B is not a pointer, so I think your notation is wrong.
you should use (*A).B instead
Nov 6 '08 #4
abasili wrote:
Chris Dollin wrote:
>`A->B` is `(*(A.B))`; the arrow has done the dereference for you.

B is not a pointer, so I think your notation is wrong.
you should use (*A).B instead
You are quite right; I fumbled my bracketing. (I wondered why I needed
the extra outer brackets ... should have wondered harder.)

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Nov 6 '08 #5
Neo
On Nov 6, 3:55 pm, Chris Dollin <chris.dol...@hp.comwrote:
abasili wrote:
Chris Dollin wrote:
`A->B` is `(*(A.B))`; the arrow has done the dereference for you.
B is not a pointer, so I think your notation is wrong.
you should use (*A).B instead

You are quite right; I fumbled my bracketing. (I wondered why I needed
the extra outer brackets ... should have wondered harder.)

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Oh my bad, so my c has gotten very rusty. Ok now if I do
my_array.word1 do I get a pointer to word1?
Nov 6 '08 #6
Neo wrote:
Oh my bad, so my c has gotten very rusty. Ok now if I do
my_array.word1 do I get a pointer to word1?
being my_array a pointer to a structure this syntax, to me, doesn't have
any meaning. Here is an example:

// test.c

#include <stdio.h>

typedef struct _t {
int a;
int b;
char *c;
} _t;

int main (int argc, char *argv[]){

_t t, *pt;
pt = &t;

t.a = 10;
t.b = 20;
t.c = "Hello!!\n";

printf ("a = %d\n", t.a);
printf ("b = %d\n", t.b);
printf ("c = %s\n", t.c);

printf ("a = %d\n", pt->a);
printf ("b = %d\n", pt->b);
printf ("c = %s\n", pt->c);

// printf ("c = %d\n", pt.a); // ERROR!

printf ("c = %d\n", &pt->a); //to get the pointer to a (it will
// complain because %d expects
//an int while &pt->a is an int *)

return 0;

}
I suggest you to read this reference:

"The C Programming Language" B.W.Kernighan & D.M.Ritchie

On chapter 6 you can find all you need.
Nov 6 '08 #7
On 6 Nov, 10:14, Neo <zingafri...@yahoo.comwrote:

the subject was:
"what is the type for this call?"
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;

} word_array;

word_array *my_array;

what would be the data type of this:
myarray->word1
there are no calls in your code so it is meaningless
to ask "what is the type for this call". And if you were
talking about a call I'd expect it to be
"what is the type *of* this call".
How come it is a uint32_t type and is giving the value of word1? isn't
it still a pointer which is not dereferenced yet?
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
--
Nick Keighley

Nov 6 '08 #8
Neo wrote:
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;

word_array *my_array;
Later, New wrote:
Oh my bad, so my c has gotten very rusty. Ok now if I do
my_array.word1 do I get a pointer to word1?
No. You defined my_array as a pointer; the '.' notation is not allowed
on a pointer. If you want a pointer to word1, the correct notation is
&my_array->word1.
Nov 6 '08 #9
Neo
On Nov 6, 4:42 pm, abasili <alessandro.bas...@cern.chwrote:
Neo wrote:
Oh my bad, so my c has gotten very rusty. Ok now if I do
my_array.word1 do I get a pointer to word1?

being my_array a pointer to a structure this syntax, to me, doesn't have
any meaning. Here is an example:

// test.c

#include <stdio.h>

typedef struct _t {
int a;
int b;
char *c;

} _t;

int main (int argc, char *argv[]){

_t t, *pt;
pt = &t;

t.a = 10;
t.b = 20;
t.c = "Hello!!\n";

printf ("a = %d\n", t.a);
printf ("b = %d\n", t.b);
printf ("c = %s\n", t.c);

printf ("a = %d\n", pt->a);
printf ("b = %d\n", pt->b);
printf ("c = %s\n", pt->c);

// printf ("c = %d\n", pt.a); // ERROR!

printf ("c = %d\n", &pt->a); //to get the pointer to a (it will
// complain because %d expects
//an int while &pt->a is an int *)

return 0;

}

I suggest you to read this reference:

"The C Programming Language" B.W.Kernighan & D.M.Ritchie

On chapter 6 you can find all you need.
I know this pointer and address mechanism but got confused when I
moved from a defines type declaration of a register to a structure
type definition where all the registers form offsets. This made my
read and write functions written for the earlier case unusable as they
reqd a uint32_t * as an argument.
for ex:
#define uart_status ((volatile unsigned int *)(UART0_BASE +
0x0))

changed to -

typedef volatile struct uart_reg_struct
{
uint32_t uart_status;
uint32_t uart_in_en;
---
---
} uart_reg_array;
uart_reg_array *uart_reg = (uart_reg_array *)0x10001000;
read_reg(uart_status);//was working earlier but now
read_reg(uart_reg->uart_status); // became a problem

Anyway, I got the idea and thanks for your feedback.
Nov 6 '08 #10
Neo
uart_reg_array *uart_reg = (uart_reg_array *)0x10001000;
read_reg(uart_status);//was working earlier but now
read_reg(uart_reg->uart_status); // became a problem

Anyway, I got the idea and thanks for your feedback.
Is there a cleaner way to deal with this struct as I have to take care
of peripheral regs (embedded world!)
The write function which required a pointer to the registers(volatile
too) becomes unwieldy with all this arrows and ampersands so I am
thinking if I should revert back to the old defines declaration.
Nov 6 '08 #11
Neo <zi*********@yahoo.comwrites:
If i have a :
typedef struct str32{
uint32_t word1;
uint32_t word2;
} word_array;

word_array *my_array;
Your naming is misleading. You use the names "struct str32" and
"word_array" for the same type -- and there's not an array in sight.
And your variable "my_array" is a pointer, not an array. I suspect
that's part of your problem; it's harder to think about things if you
haven't named them clearly.

Let's try changing the names for greater clarity. I personally prefer
not to use typedefs for structures, but I'll use one here. And since
we have a typedef, the struct tag (you used "str32") isn't necessary.
(It would be in some cases, but we'll ignore that.)

typedef struct {
uint32_t word1;
uint32_t word2;
} word_pair;

word_pair *ptr;

This declares a structure type named "word_pair" containing two
uint32_t members. We've also defined the variable "ptr" as a pointer
to word_pair object (but we haven't given it a value).

(Strictly speaking, the struct type is anonymous, and "word_pair" is
just an alias for it, but it's simpler to say that "word_pair" is the
name of the type.)

The "." operator takes a struct and a member name, and yields the
value of that member of the struct.

The "->" operator takes a pointer-to-struct and a member name, and
yields the value of that member of the struct that the pointer points
to; "p->m" is an abbreviation of "(*p).m".
what would be the data type of this:
myarray->word1
In our revised naming scheme, that would be "ptr->word1". It's of
type uint32_t.

Breaking it down, ptr is a pointer to a word_pair; presumably we've
allocated a word_pair structure somewhere and set "ptr" to point to
it.

ptr->word1 means (*ptr).word1
ptr is a pointer to a word_pair object.
*ptr is the word_pair object that it points to.
(*ptr) is the same thing.
(*ptr).word1 is the word1 member of that word_pair object. It's not
the member's address, it's the member itself.
How come it is a uint32_t type and is giving the value of word1? isn't
it still a pointer which is not dereferenced yet?
Nope. The -operator gives you the member itself, not a pointer to
it. Whatever is on the left hand side of the -must be a pointer to
struct, and is dereferenced. Whatever is on the right hand side must
be a member name. The result is the value of the member.

(You can also put it on the left hand side of an assignment:
ptr->word1 = 42;
In this case, we don't look at the previous value of the member, we
just assign a new value to it. It's just like an ordinary variable
name, except that a little more work is done to determine what object
it refers to.)
I was thinking I should have to do *(my_array->word1) to get to
uint32_t type, what goes?
Nope. If you want a pointer to the word1 member, you can write
&ptr->word1
The operator precedence is such that this means
&(ptr->word1)
But in this case, you want the value of word1, so ptr->word1 does the
trick.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Nov 6 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
15
by: XZ | last post by:
Hi everyone, this is really confusing to me: #include <stdio.h> main(int argc, char **argv) { printf("argv = %f\n",(double)atof(argv)); printf("argv = %d\n\n",atoi(argv)); } $ a.out a argv...
19
by: Fernando Cacciola | last post by:
I'm puzzled, Why and how _exactly_ is this: void Foo<T>(T v ) where T : Interface/Value/Class/class any better than this void Foo( Interface/Value/Class/object v )
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
18
by: cj | last post by:
members of this type are safe for multithreaded operations. Instance members are not guaranteed to be thread-safe. I'm under the impression before you can use a class you have to make an...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
1
nurikoAnna
by: nurikoAnna | last post by:
I am having troubled debugging with my codes...It always gets an error "TYPE MISMATCH" Below is my code in my frmApplicantsEntry: ...
1
nurikoAnna
by: nurikoAnna | last post by:
I am having troubled debugging with my codes...It always gets an error "TYPE MISMATCH" Below is my code in my frmApplicantsEntry: __________________________________________________...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.