我想知道函数的签名是什么,它接受一个函数指针并返回一个函数指针?
谢谢,
森
就像问题所说的那样,我试图将多维数组传递给函数,将其打印到工程项目的文件中.输入数据的格式不能更改,所以请不要建议我只是将其输入为不同的数据类型.
这个特殊的函数预期一个二维数组(虽然我有其他三维后的数据),直到运行时才知道数组的大小.我知道我必须使用指针分别指向数组的每一行,但我不知道将它传递给函数的语法是什么.在下面的代码中,有问题的数组是'block'.主要功能只是我尝试使其工作的一个小测试示例:
#include<fstream>
using namespace std;
void of_write_blocks(string filename, string block_type[], int **block,
int grid[][3], string grade_type[], int grade[][3], int n_blocks, int m[])
{
ofstream file_out(filename.c_str(),ios::app);
file_out<<"\nblocks\n(\n";
for(int i=0;i<n_blocks;++i) {
file_out<<" "<<block_type[i]<<" ( ";
for(int j=0;j<m[i];++j)
file_out<<block[i][j]<<" ";
file_out<<") ( ";
file_out<<grid[i][0]<<' '<<grid[i][1]<<' '<<grid[i][2]<<" ) ";
file_out<<grade_type[i]<<" ( ";
file_out<<grade[i][0]<<' '<<grade[i][1]<<' '<<grade[i][2]<<" )\n";
}
file_out<<");\n";
}
//testing example:
int main()
{
int block[6][9];
for(int i=0; i<6;++i)
for(int j=0; i<9;++j)
block[i][j] = i*j;
int grid[6][3];
for(int i=0; i<6;++i)
for(int j=0; i<3;++j) …Run Code Online (Sandbox Code Playgroud) 我有一个指针向量,指向一组Critic对象.每个评论家都有UserID,名字,姓氏等属性.
我模拟了一个修改过的quickSort,以便按每个评论家的名字对指针向量进行排序.该函数按预期工作,但仅适用于向量中的前几个实例.
void quickSortCritics(vector<Critic*> & v, int from, int to)
{
if (from < to)
{
int middle = partition(v, from, to);
quickSortCritics(v, from, middle - 1);
quickSortCritics(v, middle + 1, from);
}
}
int partition(vector<Critic*> & v, int from, int to)
{
char pivot = (v[from]->getFirstName())[0];
int left_index = from - 1;
int right_index = to + 1;
do
{
do
{
right_index--;
} while ( (v[right_index]->getFirstName())[0] > pivot);
do
{
left_index++;
} while ( (v[left_index]->getFirstName())[0] < pivot);
if …Run Code Online (Sandbox Code Playgroud) 我有一个指向指针的数组,当我这样做array [index]=classpointer;并调用时delete classpointer;,存储的值是否会array[index]变为NULL?
在table.hmysql 的代码中.有以下代码
typedef struct st_table_share
{
...
struct st_table_share * next, /* Link to unused shares */
**prev;
Run Code Online (Sandbox Code Playgroud)
在教科书中,我们通常都有
sometype *next, *prev;
Run Code Online (Sandbox Code Playgroud)
但在这里使用**prev而不是*prev.使用双指针的原因是什么prev?
我有以下课程:
class A {};
class B { vector<A> vect; };
Run Code Online (Sandbox Code Playgroud)
我可以像这样访问任意A:
A a = b.vect[0];
// A *a_ptr = &a;
Run Code Online (Sandbox Code Playgroud)
但是我如何才能直接进入*a_ptr?
A *a_ptr = &b.vet[0];
Run Code Online (Sandbox Code Playgroud)
编译并且不会给出运行时错误,但它指向错误的内存位置.
编辑:
我的真实世界的例子:http://ideone.com/kP8NK
当Ideone给出预期的" You are now at 0, 0, 0"时,MS VisualStudio编译器产生" You are now at 6624656, -33686019, -1414812757"
我正在攻读编程语言的考试,我偶然发现了这个问题(用C语言编写):
*tp++ = *sp++;
我明白了什么*tp = *sp; 可能会这样做,但在这里,指针何时递增?之前,在获取值之后?按什么顺序?我感谢你的回答
我明白这是基本的东西的一部分,但我被卡住了:-(有人可以帮帮我吗?
计划1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=1,b=2,c;
c=(a+b)++;
}
Run Code Online (Sandbox Code Playgroud)
为什么输出错误?是否需要左值?
计划2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *p1="name";
char *p2;
p2=(char*)malloc(20);
memset(p2,0,20);
while(*p2++=*p1++);
printf("%s\n",p2);
}
Run Code Online (Sandbox Code Playgroud)
为什么是输出,一个空字符串?如果我颠倒增量的顺序,那就是:while(++*p2=++*p1);为什么左值误差来了?
我编写了一个代码来读取文件,将其存储在一个结构中并显示它.但不知何故,它给了我一个分段错误,我不知道为什么.有人可以帮帮我吗?
输出:
file: /home/neel/map2.txt
file opened
Start Intersection
a->road: 4
a->roadId[0]: 1
a->lane[0][0]: 2
a->lane[0][1]: 2
a->roadId[1]: 2
a->lane[1][0]: 2
a->lane[1][1]: 2
a->roadId[2]: 3
Segmentation fault
Run Code Online (Sandbox Code Playgroud)
码:
#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
struct Intersection
{
unsigned short road;
long long int *roadId;
short *lane[2];
};
int main(int argc, char** argv)
{
std::ifstream file;
cout<<"file: "<<argv[1]<<endl;
file.open(argv[1], std::ios::in);
cout<<"file opened"<<endl;
while (!file.eof())
{
cout<<"Start Intersection"<<endl;
Intersection *a = new Intersection;
file>>a->road;
a->roadId = new long long int[a->road]; …Run Code Online (Sandbox Code Playgroud) 这段代码(从真实项目中简化)是否正确?邮件会一直打印吗?
char *cp = NULL;
char **cpp = &cp;
if(*cpp == NULL) {
printf("I believe this will this always print. Does it?\n");
}
Run Code Online (Sandbox Code Playgroud)
谢谢!