有关指针(双指针)的C / C ++问题 [英] c/c++ questions on pointers (double pointers)

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

问题描述

自从我学习c和c ++课程以来,已经有一段时间了,我对c指针感到好奇(我想在示例中使用new关键字,甚至以为我知道malloc是C方式)。我总是记得我的老师总是强迫我们使用指针,她永远不会对数组进行赋值,她向我们证明,当您使用指针而不是数组时,汇编语言中所需的命令更少。我想继续这种好的做法,但是我似乎在努力使用指针,特别是双指针。

So its been a while since I took courses on c and c++ and I'm curious on c pointers (im going to use the new keyword in my examples even thought I know malloc is the C way). I always recall my teacher always forcing us to use pointer, she would never take assignments with arrays, she proved to us that there are less commands needed in assembly language when you used pointers rather then used arrays. I want to continue this good practice but I seem to be struggling to use pointers, specifically double pounter.

让我说我想创建一个不使用c ++字符串的字库

Lets say I want to create a word bank without using the c++ string datatype and I have a double pointer of type char.

int main()
{
   string fileName = "file.txt";
   char** wordBank = null;
   int wordCount = countWords(fileName); //somefunction to get word count
}

现在我需要分配一个大的存储空间对于整个银行来说足够了,但是我不确定如何做到这一点,我相信这是真的吗?

Now I need to allocate a memory space big enough for the entire bank But im unsure on how to do this I believe it is somthing like this?

wordBank = new char*[wordCount];

现在我需要为每个单词的大小分配特定的空间,我仍然不确定。

now I need to allocate space specifilly for the size of each word, which i am still unsure aobut.

for(int i = 0; i < wordCount; i++)
{
   wordLength = getWordLength(fileName, i); // some function to get word length of each...
   //... word in the bank
   (*wordBank) = new char[wordLength];
}

好吧,我困惑的最后一部分是通过函数传递双指针。
可以说我有一个函数,可以让我操纵整个单词,可以说我只想传递单词,可以通过函数调用传递什么,以及函数定义可以传递什么。并说我想传递整个库和一个将移动指针的数字,我将通过函数调用传递的内容以及函数定义的传递内容。通常,对所有问题都感到抱歉,如果我尝试通过编写简短的程序自己回答这些问题,但是我很难将其编译。感谢收到的回复。

Okay the last part I'm confused about is passing double pointers through functions. lets say I have a function that lets me manipulate a an entire word, lets say I want to just pass the word, what would I pass through the function call and what would the function definition have. And lets say I want to pass the entire bank and a number that will move the pointer, what would I pass through the function call and what would the function definition have. Sorry for all the questions usually If I try to answer these things by myself by writing short programs but I'm having difficulty just getting it to compile. I appreciate the responses I receive.

推荐答案

分配银行:

wordBank = malloc(wordCount * sizeof(char *));

要分配单个单词:

char *addWord(char **wordBank, size_t idx, char *word) {
  wordBank[idx] = malloc(strlen(word) + 1);
  // this is how you pass a word
  strcpy(wordBank[idx], word);
  return wordBank[idx];
}

// how you pass the wordBank:
addWord(wordBand, index, someWord);

但是在汇编中包含更多指令并不一定很糟糕。恒定的开销通常在编程中不是问题。我会使用 std :: string std :: vector< string> 来花时间解决实际问题。至少不调试malloc和释放,分配和测试NULL指针。

But having more instructions in the assembly is not necessarily bad. A constant overhead is generally not a problem in programming. I would use std::string and std::vector<string> and spend my time on real problems. At least not on debugging mallocs and frees, assigning and testing NULL pointers.

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

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