指针问题 [英] Problems with Pointers

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

问题描述

Hello All


我的问题主要是如何使用/引用双指针?我现在正在试图了解''
指针''矢量的含义是什么意思?我想要做的是拿一个char数组和

将它分解为省略空格的单词。需要注意的是

,我只想使用char **和char *来实现这个目标。

因此,我从头开始创建它。下面是我到目前为止写的代码



int main()

{

char input [125] =" Hello World \ 0"

char ** doublePointer;

char * receiveArray;


doublePointer = malloc(125 * sizeof (char *));

receiveArray =输入;


int i;

for(i = 0; receiveArray [i ]!=''\'''; i ++)

{

//这里我很困惑

//我是不确定如何遍历数组并将数据存储到

//双指针/向量

//我需要这样做吗

// doublePointer [i] = malloc(sizeof(char *));

}

免费(doublePointer);


返回0;

}


关于算法/设计的任何提示和建议将是

欢迎。谢谢。

Hello All

My question mainly is how to use/reference Double Pointers? I am
currently trying to understand what the meaning of a ''vector of
pointers'' means also? What I am trying to do is take a char array and
break it up into words omitting the spaces. What needs to be noted is
that I am trying to accomplish this only using char ** and char *.
Therefore, I am creating it from scratch. Below is code that I have
written so far:

int main()
{
char input[125] = " Hello World\0"
char **doublePointer;
char *receiveArray;

doublePointer = malloc(125 * sizeof(char *));
receiveArray = input;

int i;
for(i = 0; receiveArray [ i ] != ''\0''; i++)
{
//Here I am confused
//I''m not sure how to traverse the array and store data into
// the double pointer/vector
//Do I need to do this
// doublePointer [ i ] = malloc ( sizeof( char * ));
}
free(doublePointer);

return 0;
}

Any hints and suggestions about the algorithm/design would be
welcome. Thanks.

推荐答案

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

我的问题主要是如何使用/引用双指针?我现在正在试图了解''
指针''矢量的含义是什么意思?我想要做的是拿一个char数组和

将它分解为省略空格的单词。需要注意的是

,我只想使用char **和char *来实现这个目标。

因此,我从头开始创建它。下面是我有的代码

到目前为止写的:
My question mainly is how to use/reference Double Pointers? I am
currently trying to understand what the meaning of a ''vector of
pointers'' means also? What I am trying to do is take a char array and
break it up into words omitting the spaces. What needs to be noted is
that I am trying to accomplish this only using char ** and char *.
Therefore, I am creating it from scratch. Below is code that I have
written so far:



为什么从main开始?什么功能真的,真的有助于解决这个?b $ b这个?图像存在并从那里拿走它。然后你需要做的就是

写的是现在稍微简单的功能。


就个人而言,我想要一个声明如下的函数:


char * next_word(const char ** string);


/ *返回指向
的副本(在malloc的存储中)的指针
* * string中的下一个单词。 *字符串更新为指向

*找到的第一个字符不属于返回的工作。

*如果未找到任何单词,则返回NULL。

* /


有了这个功能,我希望看到一个相对简单的

循环,放回返回的指针成为char *数组的元素。

对于奖励分数,我希望看到一个循环重新分配这个数组,如果找到了比预期更多的单词
。 br />

现在你只需要编写next_word。真的有什么功能,

真的有助于编写next_word吗?图像存在并从

那里拿走......

Why start with main? What function would really, really help to solve
this? Image it exists and take it from there. Then all you need do
is write that now slightly simpler function.

Personally, I want a function declared like this:

char *next_word(const char **string);

/* Returns a pointer to a copy (in malloc''d storage) of the
* next word found in *string. *string is updated to point to
* first character found that is not part of the work returned.
* If no word is found, NULL is returned.
*/

With this function in the bag, I''d expect to see a relatively simple
loop, putting the returned pointers into elements of a char * array.
For bonus marks, I''d hope to see a loop that reallocs this array if
more words than expected are found.

Now you just need to write next_word. What function would really,
really help to write next_word? Image it exists and take it from
there...


int main()

{

char input [125] =" Hello World \ 0"

char ** doublePointer;

char * receiveArray;


doublePointer = malloc(125 * sizeof (char *));
int main()
{
char input[125] = " Hello World\0"
char **doublePointer;
char *receiveArray;

doublePointer = malloc(125 * sizeof(char *));



如果您打算使用125,只需声明


char * doublePointer [125];


BTW,这是一个可怕的名字!用变量命名你的变量是什么意思

(通常是它们包含的)不是它们的类型。

If you are going to use 125, just declare

char *doublePointer[125];

BTW, that is a terrible name! Name you variables with what that mean
(usually what they contain) not what type they have.


receiveArray = input;


int i;

for(i = 0; receiveArray [i]!=''\'''; i ++)

{

//这里我很困惑

//我不知道如何遍历数组并将数据存储到

//双指针/向量

//我是否需要这样做

// doublePointer [i] = malloc(sizeof(char *));
receiveArray = input;

int i;
for(i = 0; receiveArray [ i ] != ''\0''; i++)
{
//Here I am confused
//I''m not sure how to traverse the array and store data into
// the double pointer/vector
//Do I need to do this
// doublePointer [ i ] = malloc ( sizeof( char * ));



我不会在主循环的中间开始,抱歉。

I would not start in the middle of a loop in main, sorry.


}


免费(doublePointer);


返回0;

}


关于算法/设计的任何提示和建议将是

欢迎。谢谢。
}
free(doublePointer);

return 0;
}

Any hints and suggestions about the algorithm/design would be
welcome. Thanks.



-

Ben。

--
Ben.


On Sun,2008年8月31日20 :00:44 -0700,stevenruiz写道:
On Sun, 31 Aug 2008 20:00:44 -0700, stevenruiz wrote:

你好所有


我的问题主要是如何使用/引用Double指针?我现在正在试图了解''
指针''矢量的含义是什么意思?我想要做的是拿一个char数组和

将它分解为省略空格的单词。需要注意的是

,我只想使用char **和char *来实现这个目标。

因此,我从头开始创建它。下面是我到目前为止写的代码



int main()

{

char input [125] =" Hello World \ 0"

char ** doublePointer;

char * receiveArray;


doublePointer = malloc(125 * sizeof (char *)); receiveArray =输入;


int i;

for(i = 0; receiveArray [i]!=''\'''; i ++){

//这里我很困惑

//我不知道如何遍历数组并将数据存储到//

双指针/矢量

//我需要这样做吗

// doublePointer [i] = malloc(sizeof(char *));

}


免费(doublePointer);


返回0;

}


欢迎任何关于算法/设计的提示和建议。

谢谢。
Hello All

My question mainly is how to use/reference Double Pointers? I am
currently trying to understand what the meaning of a ''vector of
pointers'' means also? What I am trying to do is take a char array and
break it up into words omitting the spaces. What needs to be noted is
that I am trying to accomplish this only using char ** and char *.
Therefore, I am creating it from scratch. Below is code that I have
written so far:

int main()
{
char input[125] = " Hello World\0"
char **doublePointer;
char *receiveArray;

doublePointer = malloc(125 * sizeof(char *)); receiveArray = input;

int i;
for(i = 0; receiveArray [ i ] != ''\0''; i++) {
//Here I am confused
//I''m not sure how to traverse the array and store data into //
the double pointer/vector
//Do I need to do this
// doublePointer [ i ] = malloc ( sizeof( char * ));
}
free(doublePointer);

return 0;
}

Any hints and suggestions about the algorithm/design would be welcome.
Thanks.



回答OP问题。指针向量是一个指针数组

,它实现了一个多维数组。根据我的判断,你的假设是访问

指针数组似乎是正确的。

To answer the OP question. A vector of pointers is an array of pointers
that implements a multidimensional array. Your assumption for accessing
the array of pointer seems correct as far as I can tell.


On Sun,2008年8月31日20:00: 44 -0700(PDT), st********@gmail.com 写道:
On Sun, 31 Aug 2008 20:00:44 -0700 (PDT), st********@gmail.com wrote:

> Hello All


我的问题主要是如何使用/引用双指针?我现在正在试图理解指针矢量的含义是什么意思?我想要做的是采用一个char数组和
>Hello All

My question mainly is how to use/reference Double Pointers? I am
currently trying to understand what the meaning of a ''vector of
pointers'' means also? What I am trying to do is take a char array and



这个短语在标准中没有明确的含义。由于单词

vector在C中也没有特殊含义,我会将这句话用作b>指针的同义词。

The phrase has no defined meaning in the standard. Since the word
vector also has no special meaning in C, I would take the phrase to be
synonymous with "array of pointers".


>将其分解为省略空格的单词。需要注意的是
>break it up into words omitting the spaces. What needs to be noted is



定义分解。


你想要替换单词分隔符(通常是空格)

带''\ 0'',这样阵列现在包含多个字符串,每个字符串对应一个单词?如果是这样,你想构建一个指向每个单词的

指针数组吗?


或者你想复制字符串的那些部分

构成新的char数组的单词?如果是这样,你想要

结果数组包含有效字符串还是只包含

中的字符?你想定义目标数组还是动态分配它们?

Define break it up.

Do you want to replace the word separators (usually a space)
with ''\0'' so that the array now contains multiple strings, each
corresponding to a word? If so, do you want to build an array of
pointers that points to each word?

Or do you want to copy the portions of the string that
constitute words to new arrays of char? If so, do you want the
resulting arrays to contain valid strings or just the characters in
the words? Do you want to define the target arrays or dynamically
allocate them?


>我试图用char *来实现这个目标*和char *。
>that I am trying to accomplish this only using char ** and char *.



这可能足以完成这项任务,但为什么你会随意将b $ b限制在该语言的一个子集中?通常任意

这样的限制是家庭作业的明确标志。

This is probably adequate for this task but why would you arbitrarily
limit yourself to a subset of the language? Usually arbitrary
restrictions like this are a sure sign of homework.


>因此,我从头开始创建它。下面是我到目前为止编写的代码:
>Therefore, I am creating it from scratch. Below is code that I have
written so far:



在定义intent之前编写代码是危险的。

Writing code before the intent is defined is perilous.


>

int main()

{

char input [125] =" Hello World \ 0"

char ** doublePointer;

char * receiveArray;


doublePointer = malloc(125 * sizeof (char *));
>
int main()
{
char input[125] = " Hello World\0"
char **doublePointer;
char *receiveArray;

doublePointer = malloc(125 * sizeof(char *));



你真的认为输入中需要char * char?

Do you really think you will need a char* for char in input?


receiveArray = input ;


int i;

for(i = 0; receiveArray [i]!=''\ 0''; i ++)

{

//这里我很困惑

//我不知道如何遍历数组并将数据存储到

//双指针/向量
receiveArray = input;

int i;
for(i = 0; receiveArray [ i ] != ''\0''; i++)
{
//Here I am confused
//I''m not sure how to traverse the array and store data into
// the double pointer/vector



在你确定要存储在

doublePointer指向的空间中的数据之前,我们将也不确定。

Until you decide what data to store in the space pointed to by
doublePointer, we will be unsure also.


//我是否需要这样做

// doublePointer [i] = malloc(sizeof(char *) );
//Do I need to do this
// doublePointer [ i ] = malloc ( sizeof( char * ));



这绝对是错误的。 doublePointer是一个char **。因此

doublePointer [i]是一个char *。如果你想让doublePointer [i]指向动态分配内存的
,那么传递给malloc的参数应该是

总是你打算存储的(最大)字符数

那个记忆。在大多数系统上,你的代码会将你限制在4或8.

这就是为什么许多人推荐使用以下语法来为
分配动态数组给指针的指针x:

x = malloc(number_of_elements_desired * sizeof * x);

This is definitely wrong. doublePointer is a char**. Therefore
doublePointer[i] is a char*. If you want doublePointer[i] to point to
dynamically allocated memory, the argument passed to malloc should
always be the (maximum) number of characters you intend to store in
that memory. On most system, your code would limit you to 4 or 8.
This is the reason that many here recommend the following syntax for
allocating a dynamic array to a pointer named x:
x = malloc(number_of_elements_desired * sizeof *x);


}


免费(doublePointer);
}
free(doublePointer);



如果doublePointer和所有doublePointer [i]指向分配的

内存,则需要在释放之前释放doublePointer [i]

doublePointer。

If doublePointer and all the doublePointer[i] point to allocated
memory, the doublePointer[i] need to be freed before you free
doublePointer.


>

返回0;
}
>
return 0;
}



-

删除del电子邮件

--
Remove del for email


这篇关于指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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