array

I

Ivan

Hi all,

I am trying to place a static array of structures into a memory location I
want. For example

typedef struct
{
char *string;
int i1;
int i2;
}mytype;

mytype sa[] =
{
{"string1", 0, 10},
{"string2", 0, 12},
{0}
};


Now I want the compiler/linker to place sa[] at memory address 0x20000000.

How do I go about it without copying the structure over?

thanks
ivan
 
V

viza

Hi

Now I want the compiler/linker to place sa[] at memory address
0x20000000.

How do I go about it without copying the structure over?

This cannot be done in C. Read the documentation for your assembler/
linker to find out how to do it on the platform that you are using.

viza
 
K

Kenny McCormack

Hi

Now I want the compiler/linker to place sa[] at memory address
0x20000000.

How do I go about it without copying the structure over?

This cannot be done in C. Read the documentation for your assembler/
linker to find out how to do it on the platform that you are using.

viza

Wrong. What you meant to say is "This cannot be done in comp.lang.c C" (*)
It can obviously be done in C (**)

(*) A weird specialized dialect, of little interest to anyone except to a
certain bunch of freaks, known as "clc regulars".

(**) OK, for the pedants: It can be done in "C with extensions" - that
which is known and used regularly in the real world and generally
referred to simply as (are you ready for it?) "C".
 
G

Guest

Ivan wrote:


Why does everyone use typedef like this?

they don't. About half the regulars on comp.lang.c use the
typedef and half don't. I use the typedef. I regard those
stray struct kewords floating about as ugly syntax

extern struct basestation bs;
void reset_bs (struct basestation*);

whilst with a suitable typedef it becomes

extern Basestation bs;
void reset_bs (Basestation*);

*I* think the intent is clearer. But then I was raised on Pascal
The C++ people agree with me, in C++ you don't need to use the struct
keyword when you use a type. Many clc regulars disagree with me.
Live with the fact you'll see both forms.

<snip>
 
V

viza

viza said:
Hi

Now I want the compiler/linker to place sa[] at memory address
0x20000000.

How do I go about it without copying the structure over?

This cannot be done in C. Read the documentation for your assembler/
linker to find out how to do it on the platform that you are using.

Wrong. What you meant to say is "This cannot be done in comp.lang.c C"
(*) It can obviously be done in C (**)

(*) A weird specialized dialect, of little interest to anyone except to
a certain bunch of freaks, known as "clc regulars".

(**) OK, for the pedants: It can be done in "C with extensions" - that
which is known and used regularly in the real world and generally
referred to simply as (are you ready for it?) "C".

Kenny, don't be so confrontational, :).

The fact that gcc lets you put code that doesn't even look like C in a
file with the extension .c does not make that code C. I will happily
discuss C extensions in clc, but writing assembly in a C file isn't an
extension to C, it is a completely different language.

Even then, I don't particularly mind a bit of OT if it helps the OP, but
I was not wrong to say that it cannot be done in C. (I wasn't
particularly helpful either :-S ).
 
N

Nate Eldredge

Anthony Fremont said:
Why does everyone use typedef like this?

I.e. as opposed to using something like

struct mytype { ... };

Aside from being less typing, it could in some cases be stylistically
desirable to obscure the fact that it's a struct. It could also have a
functional advantage if the type in question could someday be changed
not to be a struct, or if that could be the case in different
configurations; parts of the code that pass around objects of the type
as opaque cookies won't have to be changed. FILE is a good example of
this.

E.g.

#if defined(SYSTEM_1)
typedef int handle;
#elif defined(SYSTEM_2)
typedef struct { int a; long b; } handle;
#elif defined(SYSTEM_3)
typedef union { char *p; double x; } handle;
#endif

extern handle get_handle(void);
extern void do_something(handle h);

void foo(void) {
handle h = get_handle();
do_something(h);
}

This would not work if `foo' was written to use `struct handle'.

I think in most cases, though, it's just used to avoid typing `struct'.
 
P

Phil Carmody

Anthony Fremont said:
Not exactly what I meant. I was asking why even have the typedef at all,
it's not necessary.

struct mystruct { // I prefer my opening braces like this ;-)
char *string;
int i1;
int i2;
};

struct mystruct ms, *msp;

Is it so hard to type "struct" where it actually applies? :)

What makes you think 'hardness' enters into people's decision-
making? What a weird thing to assume. In turn, I'll assume that
people who *don't* use typedefs in such situations are weirdos.

Fair?

Phil
 
C

Chris Dollin

Anthony said:
Is it so hard to type "struct" where it actually applies? :)

If most of the types you're using are structs, the repeated repetition
of the word in, say, a formal parameterer list, makes it both harder
to understand and subject to a tendency for the
parameter list
to need
interesting layout
so that the reader can
keep track of what's going on.
 
S

S M Ryan

"Anthony Fremont said:
Not exactly what I meant. I was asking why even have the typedef at all,
it's not necessary.

struct mystruct { // I prefer my opening braces like this ;-)
char *string;
int i1;
int i2;
};

struct mystruct ms, *msp;

Is it so hard to type "struct" where it actually applies? :)

Because opaque types restrict information.

something.h
typedef struct Type Type;
something.c
struct Type {
...
};

That lets the compiler verify only something.c knows what's actually in the
struct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top