How to change the starting address of a structure element?

Discussion in 'C Programming' started by chee.k.cheng@gmail.com, Aug 25, 2005.

  1. Guest

    Hi,

    I'm wondering if it is possible to change the starting address of a
    structure element. For example, I have the following structure:

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

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

    printf("%s\n", an->alpha) prints ABCDEFGHI.
    Could anyone tell me if there is a way to change the starting address
    of the alpha element, so that printf("%s\n", an->alpha) prints EFGHI?
    (Start from E, instead of A)

    Thank you very much.
     
    , Aug 25, 2005
    #1
    1. Advertising

  2. writes:

    >Hi,


    >I'm wondering if it is possible to change the starting address of a
    >structure element. For example, I have the following structure:


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


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


    >printf("%s\n", an->alpha) prints ABCDEFGHI.
    >Could anyone tell me if there is a way to change the starting address
    >of the alpha element, so that printf("%s\n", an->alpha) prints EFGHI?
    >(Start from E, instead of A)



    Do you mean as with:

    printf("%s\n", &an->alpha[4]) ?

    --
    Chris.
     
    Chris McDonald, Aug 25, 2005
    #2
    1. Advertising

  3. writes:
    > I'm wondering if it is possible to change the starting address of a
    > structure element. For example, I have the following structure:
    >
    > struct demo {
    > char digit[10];
    > char alpha[10];
    > } an;
    >
    > strncpy(an.alpha, "ABCDEFGHI", 10);
    > strncpy(an.digit, "123456789", 10);
    >
    > printf("%s\n", an->alpha) prints ABCDEFGHI.
    > Could anyone tell me if there is a way to change the starting address
    > of the alpha element, so that printf("%s\n", an->alpha) prints EFGHI?
    > (Start from E, instead of A)


    Fortunately, no. Why would you want to do such a thing?

    What you you really trying to accomplish?

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Aug 25, 2005
    #3
  4. In article <>,
    <> wrote:
    >I'm wondering if it is possible to change the starting address of a
    >structure element.


    No.

    > For example, I have the following structure:


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


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


    >printf("%s\n", an->alpha) prints ABCDEFGHI.


    No it doesn't -- your variable "an" is not a pointer, so an->alpha
    will not work. You probably mean an.alpha

    >Could anyone tell me if there is a way to change the starting address
    >of the alpha element, so that printf("%s\n", an->alpha) prints EFGHI?
    >(Start from E, instead of A)


    No. If you want to be able to do something like that, then use

    struct demo {
    char *digit;
    char *alpha
    } an;

    an.alpha = "ABCDEFGHI";
    an.digit = "123456789";
    printf("%s\n", an.alpha);
    an.alpha += 4;
    printf("%s\n", an.alpha);


    In the above formulation, attempting to modify the characters pointed
    to by an.digit or an.alpha would result in undefined behaviour,
    as "ABCDEFGHI" is resolved to a pointer to the first element of
    an array of constant characters.

    If you want to be able to modify the characters, you would have
    to take extra steps, such as malloc'ing enough room to hold the
    string and storing the pointer in an.alpha, and then using
    strcpy or strncpy to set the contents in pretty much the same way
    you did in your sample code.
    --
    "I want to make sure [a user] can't get through ... an online
    experience without hitting a Microsoft ad"
    -- Steve Ballmer [Microsoft Chief Executive]
     
    Walter Roberson, Aug 25, 2005
    #4
  5. Guest

    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?

    Thank you very much.
     
    , Aug 25, 2005
    #5
  6. Suman Guest

    wrote:
    > Hi,
    >
    > I'm wondering if it is possible to change the starting address of a
    > structure element. For example, I have the following structure:
    >
    > struct demo {
    > char digit[10];
    > char alpha[10];
    > } an;
    >
    > strncpy(an.alpha, "ABCDEFGHI", 10);
    > strncpy(an.digit, "123456789", 10);
    >
    > printf("%s\n", an->alpha) prints ABCDEFGHI.


    ... will give you invalid type argument of `->' or somesuch.

    > Could anyone tell me if there is a way to change the starting address
    > of the alpha element, so that printf("%s\n", an->alpha) prints EFGHI?


    I don't understand this.

    > (Start from E, instead of A)


    If you don't need "ABCD" part, why are you copying them, in the first
    place?

    Or, create another structure that has your struct demo object in it
    and a char * member as:

    struct demo_foo {
    struct demo x;
    char *p;
    } t;
    ....
    strncpy(t.x.alpha, "ABCDEFGHI", 10);
    and next set the pointer to the 5th (or whatever) element of the
    other member's alpha array:
    t.p = t.x.alpha + 4;
    printf("%s\n", t.x.alpha);
    printf("%s\n", t.x.p);

    Or, if you can modify the structure a bit, add a char *,
    directly to demo and proceed as shown above.

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

    char *t;
    > } an;

    ....

    HTH.
     
    Suman, Aug 25, 2005
    #6
  7. Guest

    The real problem I'm working on has over 100 elements in the structure,
    and the element I'm interested in, alpha, has 384 characters. I need
    to traverse through alpha every 4 characters at a time to do something,
    and I'll be calling different functions to do things based on the value
    in alpha. That's why I'm looking for ways to move the pointer 4
    characters at a time. After that moving the pointer, I can simply call
    an->alpha (I'll retrieve value from an->alpha only). This way I won't
    have to keep a counter to keep track of the location in alpha. I
    simplify what I need in the following little demo program:

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

    void printstruct(struct demo *an);
    void movepointer(struct demo *an);

    int main(void)
    {
    struct demo an;

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

    printstruct(&an); /* alpha output should be ABCDEFGHI */
    movepointer(&an);
    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);
    }

    void movepointer(struct demo *an)
    {
    /* How do I advance the an->alpha pointer by 4 characters here? */
    }


    Thank you very much.
     
    , Aug 25, 2005
    #7
  8. writes:
    > The real problem I'm working on has over 100 elements in the structure,
    > and the element I'm interested in, alpha, has 384 characters. I need
    > to traverse through alpha every 4 characters at a time to do something,
    > and I'll be calling different functions to do things based on the value
    > in alpha. That's why I'm looking for ways to move the pointer 4
    > characters at a time. After that moving the pointer, I can simply call
    > an->alpha (I'll retrieve value from an->alpha only). This way I won't
    > have to keep a counter to keep track of the location in alpha. I
    > simplify what I need in the following little demo program:


    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.

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

    > struct demo
    > {
    > char digit[10];
    > char alpha[10];
    > };
    >
    > void printstruct(struct demo *an);
    > void movepointer(struct demo *an);
    >
    > int main(void)
    > {
    > struct demo an;
    >
    > strncpy(an.alpha, "ABCDEFGHI", 10);
    > strncpy(an.digit, "123456789", 10);
    >
    > printstruct(&an); /* alpha output should be ABCDEFGHI */
    > movepointer(&an);
    > 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);
    > }
    >
    > void movepointer(struct demo *an)
    > {
    > /* How do I advance the an->alpha pointer by 4 characters here? */
    > }


    You're using the name "an" both for an object of type "struct demo"
    and for a parameter of type "struct demo*".

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

    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;

    --
    Keith Thompson (The_Other_Keith) <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.
     
    Keith Thompson, Aug 25, 2005
    #8
  9. Suman Guest

    wrote:
    > The real problem I'm working on has over 100 elements in the structure,
    > and the element I'm interested in, alpha, has 384 characters. I need
    > to traverse through alpha every 4 characters at a time to do something,
    > and I'll be calling different functions to do things based on the value
    > in alpha. That's why I'm looking for ways to move the pointer 4
    > characters at a time. After that moving the pointer, I can simply call
    > an->alpha (I'll retrieve value from an->alpha only). This way I won't
    > have to keep a counter to keep track of the location in alpha. I
    > simplify what I need in the following little demo program:
    >


    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.

    You also need things like:

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

    > struct demo
    > {
    > char digit[10];
    > char alpha[10];
    > };
    >
    > void printstruct(struct demo *an);
    > void movepointer(struct demo *an);
    >
    > int main(void)
    > {
    > struct demo an;
    >
    > strncpy(an.alpha, "ABCDEFGHI", 10);
    > strncpy(an.digit, "123456789", 10);
    >
    > printstruct(&an); /* alpha output should be ABCDEFGHI */
    > movepointer(&an);
    > 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);
    > }
    >
    > void movepointer(struct demo *an)
    > {
    > /* How do I advance the an->alpha pointer by 4 characters here? */

    /* 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!
     
    Suman, Aug 25, 2005
    #9
  10. Guest

    wrote:
    >struct demo
    >{
    > char digit[10];
    > char alpha[10];

    here, "alpha" is the name of an array. you can't change it's value.


    >};



    >void printstruct(struct demo *an);
    >void movepointer(struct demo *an);


    >int main(void)
    >{
    > struct demo an;



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



    > printstruct(&an); /* alpha output should be ABCDEFGHI */
    > movepointer(&an);
    > 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);



    >}



    >void movepointer(struct demo *an)
    >{
    > /* How do I advance the an->alpha pointer by 4 characters here? */


    "alpha" is the name of an array, do you want to changes its value?

    >}

    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;
    }
     
    , Aug 25, 2005
    #10
  11. 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
     
    Lawrence Kirby, Aug 25, 2005
    #11
  12. pete Guest

    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
     
    pete, Aug 25, 2005
    #12
  13. Guest

    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.
     
    , Aug 26, 2005
    #13
  14. Guest

    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.
     
    , Aug 26, 2005
    #14
  15. Guest

    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.
     
    , Aug 26, 2005
    #15
  16. Guest

    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.
     
    , Aug 26, 2005
    #16
  17. 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) <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.
     
    Keith Thompson, Aug 26, 2005
    #17
  18. 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) <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.
     
    Keith Thompson, Aug 26, 2005
    #18
  19. CBFalconer Guest

    Keith Thompson wrote:
    > 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Aug 26, 2005
    #19
  20. Suman Guest

    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.
     
    Suman, Aug 26, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. candide
    Replies:
    65
    Views:
    1,452
  2. John Koleszar

    address of static array element as address constant

    John Koleszar, Jul 7, 2008, in forum: C Programming
    Replies:
    7
    Views:
    646
    John Koleszar
    Jul 15, 2008
  3. Replies:
    3
    Views:
    314
    Ben Bacarisse
    Sep 20, 2008
  4. Vincent De Groote

    structure address = structure first field address ?

    Vincent De Groote, Apr 29, 2009, in forum: C Programming
    Replies:
    31
    Views:
    2,097
  5. Fred

    Re: Address of structure element

    Fred, Sep 17, 2009, in forum: C Programming
    Replies:
    4
    Views:
    304
    James Kuyper
    Sep 18, 2009
Loading...

Share This Page