如何更改结构元素的起始地址? [英] How to change the starting address of a structure element?

查看:66
本文介绍了如何更改结构元素的起始地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道是否可以更改

结构元素的起始地址。例如,我有以下结构:


struct demo {

char digit [10];

char alpha [10 ];

} an;


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

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


printf("%s \ n",an-> alpha)打印ABCDEFGHI。

有人能告诉我是否有办法改变起始地址

的alpha元素,以便printf(%s \ n,an-> alpha)打印出EFGHI?

(从E开始,而不是A)


非常感谢。

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.

推荐答案

ch ********** @ gmail.com 写道:
ch**********@gmail.com writes:

我想知道是否可以更改
结构元素的起始地址。例如,我有以下结构:
struct demo {
char digit [10];
char alpha [10];
};
strncpy(an.alpha," ABCDEFGHI",10);
strncpy(an.digit," 123456789",10);
printf("%s \ n",an-> alpha)打印ABCDEFGHI。
有人能告诉我是否有办法更改alpha元素的起始地址
,printf(%s \ n,an-> alpha)打印EFGHI?
(从E开始,而不是A)
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)



你们意思是:


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


-

克里斯。


Do you mean as with:

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

--
Chris.


ch ********** @ gmail.com 写道:
我想知道是否可以更改
结构元素。例如,我有以下结构:

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

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

printf("% s \ n",an-> alpha)打印ABCDEFGHI。
任何人都可以告诉我是否有办法更改alpha元素的起始地址
,以便printf("%s) \ n",an-> alpha)打印EFGHI?
(从E开始,而不是A)
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)




幸运的是,没有。你为什么要做这样的事情?


你真正想要完成的是什么?


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> ;

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



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.


在文章< 11 ********************* @ g44g2000cwa .googlegroups。 com>,

< ch ********** @ gmail.com>写道:
In article <11*********************@g44g2000cwa.googlegroups. com>,
<ch**********@gmail.com> wrote:
我想知道是否可以更改
结构元素的起始地址。


No.

例如,我有以下结构:
struct demo {
char digit [10];
char alpha [10];
};
strncpy(an.alpha," ABCDEFGHI",10);
strncpy(an.digit," 123456789",10);
printf("%s \ n",an-> alpha)打印ABCDEFGHI。


不,它不是 - 你的变量an不是指针,所以an-> alpha

将不起作用。你可能意味着an.alpha

任何人都可以告诉我是否有办法改变alpha元素的起始地址
,以便printf("%s \ n", an-> alpha)打印EFGHI?
(从E开始,而不是A)
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)




编号如果你想能够做类似的事情然后使用


struct demo {

char * digit;

char * alpha

} a;


an.alpha =" ABCDEFGHI";

an.digit =" 123456789";

printf ("%s \ n",an.alpha);

an.alpha + = 4;

printf("%s \ n",an .alpha);

在上面的公式中,试图通过an.digit或an.alpha修改指向

的字符会导致未定义的行为,

为ABCDEFGHI解析为指向

的第一个元素的指针是一个常量字符数组。


如果你想能够修改字符,你会有

采取额外的步骤,例如malloc''足够的空间来保存

字符串并将指针存储在an.alpha中,然后使用

strcpy或strncpy以几乎相同的方式设置内容

你在示例代码中做了。

-

" ;我想确保[一个用户]无法通过......一个没有点击微软广告的在线

体验

- 史蒂夫鲍尔默[微软]行政长官]



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]


这篇关于如何更改结构元素的起始地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆