473,320 Members | 1,856 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,320 software developers and data experts.

How to change the starting address of a structure element?

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.

Nov 15 '05 #1
19 2568
ch**********@gmail.com 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.
Nov 15 '05 #2
ch**********@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #3
In article <11*********************@g44g2000cwa.googlegroups. com>,
<ch**********@gmail.com> 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]
Nov 15 '05 #4
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.

Nov 15 '05 #5

ch**********@gmail.com 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.

Nov 15 '05 #6
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.

Nov 15 '05 #7
ch**********@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #8

ch**********@gmail.com 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!

Nov 15 '05 #9
chee.k.ch...@gmail.com 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;
}

Nov 15 '05 #10
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

Nov 15 '05 #11
ch**********@gmail.com 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
Nov 15 '05 #12

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.

Nov 15 '05 #13
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.

Nov 15 '05 #14

ke******@hotmail.com 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.

Nov 15 '05 #15

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.

Nov 15 '05 #16
ch**********@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #17
ch**********@gmail.com 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) ks***@mib.org <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.
Nov 15 '05 #18
Keith Thompson wrote:
ch**********@gmail.com 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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 15 '05 #19

ch**********@gmail.com 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.

Nov 15 '05 #20

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

Similar topics

27
by: Adam Warner | last post by:
Hi all, In the code snippet below I successfully determine the address of val1:* struct o val1=l_SYM_2B(&a).o; print_aesthetic(&val1); The structure o is heavyweight. I understand...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
1
by: Hemant Mohan | last post by:
Consider the following program snipet: <snip> typedef struct { unsigned char a ; unsigned char b ; unsigned char c ;
12
by: FI | last post by:
Hello All, I am relatively new to C programming and I am struck with a problem in dynamic memory allocation. I would like to know if it is ok to pass the 'memory address' returned by...
6
by: Jakob Bieling | last post by:
Hi, I want to move an element from a std::list to the end of the same list. To get this done, I thought I'd just do something like: std::list <intlst; lst.push_back (0); lst.push_back (1);...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
36
by: Ajay | last post by:
Hi All, I got a segmentation fault while accessing a structure element. I pasted the output from gdb and the backtrace. However if I am able successfully access the structure using the...
0
by: Mr.SpOOn | last post by:
I think I've found a nice way to represent and build chords. At least, at the moment it satisfy me, maybe later I'll understand how it sucks. I'm using two separate classes: one represent a chord...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.