我从C中断了一下,我又回到了原点.
如果我想创建一个二维的二维数组,我可以用两种方式做到:
double** m_array = (double**) malloc(2*sizeof(double*));
double* m_array = (double*) malloc(2*sizeof(double));
Run Code Online (Sandbox Code Playgroud)
要么
double array[2][2];
Run Code Online (Sandbox Code Playgroud)
但是,当我希望传递malloc'd数组而不是传递另一个时,似乎有两个约定:
//allowed for passing in malloc'd array, but not for other array
func_m(m_array) //allowed
func_m(array) //disallowed
func_m(double** m_array)
//allowed for passing in either array; required for passing in non-malloc'd array
func(m_array) //allowed
func(array) //allowed
func(double array[][2])
Run Code Online (Sandbox Code Playgroud)
在第一个中,我不需要任何信息,除了它是一个指针数组的指针.但它只能是一个malloc阵列.
在第二个中,我需要传递double*指向的数组的每个数组的长度.这看起来很傻.
我错过了什么吗?提前致谢.
当我不能通过引用传递参数并且我需要使用指针时,你能给我一个例子.我找到了一个例子,但我不确定.
假设您有一个D派生自基类的类B.如果你想这样做,你需要指针:
void function(B* b){...}
int main{
D* d;
function(d);
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个TextField类,它目前存储对变量的引用,并且每当程序员想要时,它们都可以调用TextField.show(),并且将在屏幕上读取和显示引用的变量.
但是,如果引用无效,则会导致问题.例如,如果引用的变量超出范围,我就遇到了问题.
基本上,有没有办法确保用户提供一个不会失效的引用?或者,有没有更好的方法来设置这个系统?或者如果没有,在这种情况下处理无效引用的最佳方法是什么?
现在我有一个指针设置在我的2D数组中的一行.我希望该指针停止指向该行,但我稍后将使用指针进行其他操作.我只是想知道如何在指针初始化并指向一行后取消设置指针.
double* tempRow;
tempRow = (double*) malloc(size * sizeof(double));
...
tempRow = NULL;
Run Code Online (Sandbox Code Playgroud)
不会将tempRow变量与数组行取消链接.为什么不?
我想知道我是否应该使用C代替.使用矢量时会有开销吗?
结构看起来像这样:
template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node *add_on)
{
entry = item;
next = add_on;
}
Run Code Online (Sandbox Code Playgroud)
并且*new_rear指针没有初始化,但是&item填充了用户输入.
Error_code Extended_queue::append(const Queue_entry &item) {
Node<Queue_entry> *new_rear = new Node<Queue_entry>(item);
if(new_rear = 0)
return overflow;
if(rear = 0){
front = new_rear;
rear = new_rear;
}
else {
rear->next = new_rear;
rear = new_rear;
}
return success;
}
Run Code Online (Sandbox Code Playgroud)
在VS2010的本地人中,这个和new_rear都是(!)在下一个和条目中,项目是好的.我该怎么做才能得到这个?
"访问冲突写入位置0x00000010."
1) if(null != parentObj.childObj)
2) if(parentObj.childObj != null)
在"parentObj"为空的情况下,你是否认为"1"会避免潜在的空指针异常,而不是"2"?
所以,当我将一个const char *函数传递给一个函数时,我可以再次使用它吗?它似乎最终吐出废话给我.
const char *config_file = "file.txt";
function(int x, config_file);
cout << "Number" << x;
secondfunction(int y, config_file);
Run Code Online (Sandbox Code Playgroud)
我需要另一个指向config_file的指针吗?
如果是这样,我该怎么做?
谢谢!
只是尝试制作一种哈希表,每个节点都是一个链表.
刚刚初始化空间时遇到麻烦,我做错了什么?
#include <stdlib.h>
typedef struct entry {
struct entry *next;
void *theData;
} Entry;
typedef struct HashTable {
Entry **table;
int size;
} HashTable;
int main(){
HashTable *ml;
ml = initialize();
return 0;
}
HashTable *initialize(void)
{
HashTable *p;
Entry **b;
int i;
if ((p = (HashTable *)malloc(sizeof(HashTable *))) == NULL)
return NULL;
p->size = 101;
if ((b = (Entry **)malloc(p->size * sizeof(Entry **))) == NULL)
return NULL;
p->table = b;
for(i = 0; i < p->size; i++) { …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,使用libxml库:
key = xmlNodeListGetString(doc, cur3->xmlChildrenNode, 1);
if (flag == 1)
{
image2 = key;
printf("the image 2 is %s \n", image2);
flag = 2;
}
if(flag == 0)
{
image1 = key;
printf("the image 1 is %s \n", image1);
flag = 1;
}
//printf("SRC of the file is: %s\n", key);
xmlFree(key);
printf("the image 1 is %s \n", image1);
Run Code Online (Sandbox Code Playgroud)
两个printf给了我不同的输出.
输出是:
the image 1 is 1.png
the image 1 is 0p? g
the image 2 is 2.png
the image 1 is …Run Code Online (Sandbox Code Playgroud) 哪一个是最好的写作方式:
string* str
Run Code Online (Sandbox Code Playgroud)
要么:
string *str
Run Code Online (Sandbox Code Playgroud)
其中一个有副作用的缺点吗?
谢谢