Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to change the starting address of a structure element?

Reply
Thread Tools

How to change the starting address of a structure element?

 
 
Lawrence Kirby
Guest
Posts: n/a
 
      08-25-2005
On Wed, 24 Aug 2005 23:44:49 -0700, chee.k.cheng wrote:

> Hi,
>
> Actually, I have a function called
> void movepointer(struct demo *an);
> In this function, I would like to change the starting address of
> an->alpha to point to E. Once I do that, I should be able to call
> an->alpha anywhere and it should point to E. The question of how to
> move to pointer to point to E, or point to G for that matter?


an->alpha isn't a pointer it is an array, it doesn't contain any pointer
that can be changed. The address of any object in C is foxed for that
objects lifetime. If you want a pointer that can point to various parts of
an->alpha then create a separate pointer member in the structure, set
it to point to the firast element of an->alpha and have movepointer()
modify that pointer member.

Lawrence

 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> That's why I'm looking for ways to move the pointer 4
> characters at a time.


Why don't you try using a pointer?

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>

struct demo
{
char digit[10];
char alpha[10];
};

int main(void)
{
struct demo an;
char *a_ptr = an.alpha;
char *d_ptr = an.digit;

strncpy(an.alpha, "ABCDEFGHI", 10);
strncpy(an.digit, "123456789", 10);

puts(a_ptr); /* alpha output should be ABCDEFGHI */
a_ptr += 4;
puts(a_ptr); /* alpha output should be EFGHI, and it is */
return 0;
}

/* END new.c */


--
pete
 
Reply With Quote
 
 
 
 
chee.k.cheng@gmail.com
Guest
Posts: n/a
 
      08-26-2005

Keith Thompson wrote:
> First, search this newsgroup for the phrase "Context, dammit!". You
> will find advice about how to post properly using groups.google.com.
> Please follow it.


I'm new to newsgroup sorry. I hope I'm doing it right this time.

> A matter of terminology: you don't "call" an->alpha. Only functions
> can be called.


I'll keep that in mind. Thank you.

> an.alpha, or an->alpha, is not a pointer. It's an array, and you
> can't change its address.


Thank you for pointing it out. I understand now.

> You might try adding a member to your structure to represent the
> current index you're interested in. For example (untested fragmentary
> code):
>
> struct demo {
> char alpha[384];
> size_t alpha_index;
> }
>
> ...
>
> strcpy(demo.alpha, "whatever");
> demo.alpha_index = 0;
>
> printf("Alpha = %.10s\n", demo.alpha + demo.alpha_index);
> demo.alpha_index += 4;


This will work. Thank you.

 
Reply With Quote
 
chee.k.cheng@gmail.com
Guest
Posts: n/a
 
      08-26-2005
Suman wrote:
> If you don't quote enough context, it is difficult to surmise to
> which post(er) you are replying. See Keith's reply, and follow that.


I'm new to newsgroup and didn't know the proper way to do it. I hope
I'm doing it right this time. Thanks for the advice.

> /* you can't, really! so the other way */
> char *walker = an->alpha;
> for ( ; *walker != 0; walker += 4 )
> {
> /* whatever, on earth & heaven, Horatio, you can dream of */
> }
>
> The thing is: had you read my earlier post a little more carefully,
> we wouldn't have been seeing this threads. Alas!


I didn't quite understand your earlier post when I first read it. I
understand it now when I read it again. I ended up using solution from
your earlier post and this post. Thank you very much.

 
Reply With Quote
 
chee.k.cheng@gmail.com
Guest
Posts: n/a
 
      08-26-2005

(E-Mail Removed) wrote:
> here, "alpha" is the name of an array. you can't change it's value.


I finally got it. Thank you.

> maybe the following program can meet your requirement:
> #include <stdio.h>
> #include <stdlib.h>
>
> struct demo
> {
> char *digit;
> char *alpha;
>
> };
>
>
> void printstruct(struct demo *an);
> void movepointer(struct demo *an);
>
> int main(void)
> {
> struct demo an;
>
> an->alpha = (char *)malloc(384 * sizeof(char));
> an->digit = (char *)malloc(10 * sizeof(char));
>
> strncpy(an.alpha, "ABCDEFGHI", 10);
> strncpy(an.digit, "123456789", 10);
>
>
> printstruct(&an); /* alpha output should be ABCDEFGHI */
> an->alpha = movepointer(&an, 4);
> printstruct(&an); /* alpha output should be EFGHI */
>
>
>
> }
>
>
> void printstruct(struct demo *an)
> {
> printf("Alpha\t = %.10s\n", an->alpha);
> printf("Numeric\t = %.10s\n", an->digit);
>
>
> }
>
>
> char * movepointer(struct demo *an, size_t move_index)
> {
> an->alpha += move_index;
> return an->alpha;
> }


This would work for me. Thank you.

 
Reply With Quote
 
chee.k.cheng@gmail.com
Guest
Posts: n/a
 
      08-26-2005

pete wrote:
> Why don't you try using a pointer?
>
> /* BEGIN new.c */
>
> #include <stdio.h>
> #include <string.h>
>
> struct demo
> {
> char digit[10];
> char alpha[10];
> };
>
> int main(void)
> {
> struct demo an;
> char *a_ptr = an.alpha;
> char *d_ptr = an.digit;
>
> strncpy(an.alpha, "ABCDEFGHI", 10);
> strncpy(an.digit, "123456789", 10);
>
> puts(a_ptr); /* alpha output should be ABCDEFGHI */
> a_ptr += 4;
> puts(a_ptr); /* alpha output should be EFGHI, and it is */
> return 0;
> }
>
> /* END new.c */


This would work. Thank you.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-26-2005
(E-Mail Removed) writes:
> Keith Thompson wrote:
>> First, search this newsgroup for the phrase "Context, dammit!". You
>> will find advice about how to post properly using groups.google.com.
>> Please follow it.

>
> I'm new to newsgroup sorry. I hope I'm doing it right this time.


Yes, you are. Thank you very much. (We see way to many people here
who seem unwilling to learn.)

[snip]

>> strcpy(demo.alpha, "whatever");
>> demo.alpha_index = 0;
>>
>> printf("Alpha = %.10s\n", demo.alpha + demo.alpha_index);
>> demo.alpha_index += 4;

>
> This will work. Thank you.


Glad to hear it. You're welcome.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-26-2005
(E-Mail Removed) writes:
> pete wrote:
>> Why don't you try using a pointer?

[snip]
>
> This would work. Thank you.


Just one minor comment: It's not necessary to respond to everyone who
posted on the thread. A single "Thanks, everyone" is more than
sufficient unless you want to post more specific comments.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-26-2005
Keith Thompson wrote:
> (E-Mail Removed) writes:
> > Keith Thompson wrote:

>
>>> First, search this newsgroup for the phrase "Context, dammit!".
>>> You will find advice about how to post properly using
>>> groups.google.com. Please follow it.

> >
> > I'm new to newsgroup sorry. I hope I'm doing it right this time.

>
> Yes, you are. Thank you very much. (We see way to many people
> here who seem unwilling to learn.)


And then every once in a while we get someone like Mr. Cheng who
catches on and makes all the ranting and raving worthwhile. This
prevents us from simply blacklisting all posters from google
groups.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
Suman
Guest
Posts: n/a
 
      08-26-2005

(E-Mail Removed) wrote:
> Suman wrote:
> > If you don't quote enough context, it is difficult to surmise to
> > which post(er) you are replying. See Keith's reply, and follow that.

>
> I'm new to newsgroup and didn't know the proper way to do it. I hope
> I'm doing it right this time. Thanks for the advice.


And you've been rather quick to learn. Congratulations!

>
> > /* you can't, really! so the other way */
> > char *walker = an->alpha;
> > for ( ; *walker != 0; walker += 4 )
> > {
> > /* whatever, on earth & heaven, Horatio, you can dream of */
> > }
> >
> > The thing is: had you read my earlier post a little more carefully,
> > we wouldn't have been seeing this threads. Alas!

>
> I didn't quite understand your earlier post when I first read it. I
> understand it now when I read it again. I ended up using solution from
> your earlier post and this post. Thank you very much.


Well, all said and done, come back whenever you've a problem.
And check out the FAQ (if you haven't already).

Wish you all the luck.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
structure address = structure first field address ? Vincent De Groote C Programming 31 05-19-2009 04:09 AM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C++ 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui C Programming 0 04-27-2009 12:51 PM
3 ESSENTIAL TOOLS FOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLSFOR STARTING AND MAINTAINING...3 ESSENTIAL TOOLS FOR STARTING ANDMAINTAINING... Oanh Bui Python 0 04-27-2009 12:46 PM



Advertisments