如何创建单独的字符指针? [英] How to create separate character pointers?

查看:78
本文介绍了如何创建单独的字符指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建单独的字符指针,

将它们传递给函数以分配每个不同的值,

并将指针指定给数组。


但是当我尝试时:


#include< stdio.h>

#include< string .h>


int main(无效)

{

int i;


for(i = 0; i< 5; ++ i)

{

char * str =" test";

printf("%p:%s \ n",str,str);

}


返回0;

}


所有指针都指向相同的地址。

当''char * str =" test"''运行时,没有新指针是否已创建?

解决方案

Lambda写道:


I ''想创建单独的字符指针,

将它们传递给函数以分配每个不同的值,

并指定点我要去尝试:


#include< stdio.h>

#include< string.h>


int main(无效)

{

int i;


for(i = 0;我< 5; ++ i)

{

char * str =" test";

printf("%p:%s \ n" ;,str,str);

}


返回0;

}


所有指针都指向相同的地址。

当''char * str =" test"''运行时,没有创建新的指针?



循环的每次执行都会创建一个新的'str''变量。

每次,该变量初始化为指向同一个变量

" test"串。如果你的电话号码写在五张纸上,那并不代表你有五种不同的b $ b b电话。


-

Eric Sosman
es ***** @ ieee- dot-org.inva lid


Lambda写道:


我想要创建单独的字符指针,

将它们传递给函数以分配每个不同的值,

并将指针指定给数组。


但是当我尝试时:


#include< stdio.h>

#include< string.h>


int main(无效)

{

int i;


for(i = 0; i < 5; ++ i)

{

char * str =" test";

printf("%p:% s \ n",str,str);

}


返回0;

}


所有指针都指向相同的地址。

当''char * str =" test"''运行时,没有创建新的指针?



每次都会创建一个新的指针对象,尽管在很多

实现中可能会在

完全相同的位置。关键问题不是指针对象,

它是用它初始化的指针值。当你写一个

字符串文字,例如test在这样的情况下,会发生什么是

创建一个包含5个字符的未命名数组,并用t填充,

''e'', '''','''和''\'''。字符串文字被视为指向

该数组的指针。因此,每次循环时,你都会用指针值初始化你的指针对象,该指针值指向

同样未命名的数组。

解决这个问题的正确方法在很大程度上取决于你想要做什么的b $ b。符合您描述的最简单的方法是:
如下:


#include< stdio.h>

int main (无效)

{

char str1 [] =" test";

char str2 [] =" test";

char str3 [] =" test";

char str4 [] =" test";

char str5 [] =" test" ;;

char * array [5] = {str1,str2,str3,str4,str5};

int i;


for(i = 0; i< 5; ++ i)

printf("%p:%s \ n",array [i],array [i]);

//使用数组。


返回0;

}


注意在这种情况下,字符串文字用于初始化一个

数组,而不是数组中的指针。在这种情况下,没有未命名的数组是

创建的;相反,每个定义创建一个单独的数组和

初始化该数组


虽然上面的代码符合你的描述,但我怀疑它是什么

你真的想要。以下更复杂的案例可能更接近你要找的东西:


#include< stdio.h>

#include< stdlib.h>

#define STRINGS 5

int main(无效)

{

char * array [STRINGS];

int i;

for(i = 0; i< STRINGS; i ++)

{

array [i] = malloc(5);

if(array [i])

{

strcpy(array [i]," test");

printf("%p:%s \ n",array [i],array [i]);

}

其他

{

printf("%p:failed allocation\\\
,数组[i]);

}

}


for(i = 0; i< STRINGS; i ++)

免费(array [i]);

返回0;

}


James Kuyper写道:


>



.... snip ...


>

for(i = 0; i< 5; ++ i)

printf("%p:%s \ n",array [i],array [i]);

//使用数组。



为避免尴尬的换行,请避免在发布的

材料中使用//注释。它们在C90下也是非法的。


-

Chuck F(cinefalconer at maineline dot net)

< http: //cbfalconer.home.att.net>

尝试下载部分。


-

通过免费的Usenet发布来自 http://www.teranews.com 的帐户


I''d like to create separate character pointers,
pass them to a function to assign each one different value,
and assign the pointers to an array.

But when I try:

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

int main(void)
{
int i;

for (i = 0; i < 5; ++i)
{
char *str = "test";
printf("%p:%s\n", str, str);
}

return 0;
}

All the pointers refer to the same address.
When the ''char *str = "test"'' is run, no new pointer is created?

解决方案

Lambda wrote:

I''d like to create separate character pointers,
pass them to a function to assign each one different value,
and assign the pointers to an array.

But when I try:

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

int main(void)
{
int i;

for (i = 0; i < 5; ++i)
{
char *str = "test";
printf("%p:%s\n", str, str);
}

return 0;
}

All the pointers refer to the same address.
When the ''char *str = "test"'' is run, no new pointer is created?

Each execution of the loop creates a new `str'' variable.
Each time, that variable is initialized to point to the same
"test" string. If your telephone number is written on five
pieces of paper, it doesn''t mean you have five different
telephones.

--
Eric Sosman
es*****@ieee-dot-org.invalid


Lambda wrote:

I''d like to create separate character pointers,
pass them to a function to assign each one different value,
and assign the pointers to an array.

But when I try:

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

int main(void)
{
int i;

for (i = 0; i < 5; ++i)
{
char *str = "test";
printf("%p:%s\n", str, str);
}

return 0;
}

All the pointers refer to the same address.
When the ''char *str = "test"'' is run, no new pointer is created?

A new pointer object is created each time, though on many
implementations that new pointer object is likely to be created in
exactly the same location. The key problem isn''t the pointer object,
it''s the pointer value that it''s initialized with. When you write a
string literal such as "test" in contexts like this one, what happens is
that an unnamed array of 5 characters is created and filled in with ''t'',
''e'', ''s'', ''t'', and ''\0''. The string literal is treated as a pointer to
that array. Therefore, every time you go through the loop, you
initialize your pointer object with a pointer value that points at the
same unnamed array.

The right way to fix this depends very much on what it is that you''re
trying to do. The simplest approach that matches your description would
be as follows:

#include <stdio.h>
int main(void)
{
char str1[] = "test";
char str2[] = "test";
char str3[] = "test";
char str4[] = "test";
char str5[] = "test";
char *array[5] = {str1, str2, str3, str4, str5};
int i;

for(i=0; i<5; ++i)
printf("%p:%s\n", array[i], array[i]);
// Use array.

return 0;
}

Note that in this case the string literals are used to initialize an
array, not a pointer in an array. In this case, no unnamed array is
created; instead, each definition creates a separate array and
initializes that array

While the above code matches your description, I doubt that it''s what
you really want. The following more complicated case is probably closer
to what you''re looking for:

#include <stdio.h>
#include <stdlib.h>
#define STRINGS 5

int main(void)
{
char *array[STRINGS];
int i;
for(i=0; i<STRINGS; i++)
{
array[i] = malloc(5);
if(array[i])
{
strcpy(array[i], "test");
printf("%p:%s\n", array[i], array[i]);
}
else
{
printf("%p: failed allocation\n", array[i]);
}
}

for(i=0; i<STRINGS; i++)
free(array[i]);
return 0;
}


James Kuyper wrote:

>

.... snip ...

>
for(i=0; i<5; ++i)
printf("%p:%s\n", array[i], array[i]);
// Use array.

To avoid awkward line wraps, avoid using the // comments in posted
material. They are also illegal under C90.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com


这篇关于如何创建单独的字符指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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