在这个问题,有人建议意见,我应该不会投的结果malloc,即
int *sieve = malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)
而不是:
int *sieve = (int *) malloc(sizeof(int) * length);
Run Code Online (Sandbox Code Playgroud)
为什么会这样呢?
我正在阅读Kernighan和Richie的C编程语言中有关数组和指针的章节.
他们给出了一个例子:
/* strlen: return length of string s */
int strlen(char *s)
{
int n;
for (n = 0; *s != '\0'; s++)
n++;
return n;
}
Run Code Online (Sandbox Code Playgroud)
然后说:
"既然s是一个指针,增加它是完全合法的; s++对调用的函数中的字符串没有影响strlen,但只增加了strlen指针的私有副本.这意味着像这样的电话
strlen("hello, world"); /* string constant */
strlen(array); /* char array[100]; */
strlen(ptr); /* char *ptr; */
Run Code Online (Sandbox Code Playgroud)
所有的工作."
除了第一个调用示例之外,我觉得我理解所有这些:为什么或如何将字符串文字"hello, world"视为char *s?这怎么指针?函数是否将此字符串文字指定为其局部变量的值,*s然后s用作数组名称/指针?
我觉得C中的三分指针被视为"坏".对我来说,有时使用它们是有道理的.
从基础开始,单指针有两个目的:创建数组,并允许函数更改其内容(通过引用传递):
char *a;
a = malloc...
Run Code Online (Sandbox Code Playgroud)
要么
void foo (char *c); //means I'm going to modify the parameter in foo.
{ *c = 'f'; }
char a;
foo(&a);
Run Code Online (Sandbox Code Playgroud)
在双指针可以是2D阵列(或阵列的阵列中,由于每个"列"或"行"不必具有相同的长度).我个人喜欢在需要传递一维数组时使用它:
void foo (char **c); //means I'm going to modify the elements of an array in foo.
{ (*c)[0] = 'f'; }
char *a;
a = malloc...
foo(&a);
Run Code Online (Sandbox Code Playgroud)
对我来说,这有助于描述foo正在做什么.但是,没有必要:
void foo (char *c); //am I modifying a char or just passing a char array?
{ c[0] = …Run Code Online (Sandbox Code Playgroud) 是的,我已经阅读了这个问题和答案:在C中通过引用传递数组?.我有类似的问题,并从该问题实现了相同的想法.
但是,我仍然从以下代码中得到错误:
#include <iostream>
void FillArray(int** myArray)
{
free( *myArray ) ;
* myArray = (int*) malloc(sizeof(int) * 2);
*myArray[0] = 1;
*myArray[1] = 2;
}
int main()
{
int* myArray = NULL;
FillArray(& myArray);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
FillArray函数结束后,我收到了以下运行时错误:
Program.exe 0xC00000005中0x773115de处的未处理异常:访问冲突写入位置0xcccccccccc.
我正在使用Visual Studio,打开Visual C++ Empty Project.并且文件名为main.cpp.这是否意味着它是用C++编译器而不是C编译器编译的?如果是这样,我怎样才能打开一个只会编译C编译器的文件?我尝试用main.c重命名main.cpp,但仍然有同样的问题.(我问这个问题,因为我读了一些关于"通过引用传递"的内容,并发现它在C和C++中有所不同.)
对不起这个非常基本的问题.
如有任何帮助,我将不胜感激,
SAIT.
我的问题是如何将struct.variable(或结构数组)传递给void函数.基本上代码如下:
结构
struct Person{
string surname;
string BType;
string organ;
int age;
int year, ID, IDp;
} Patient[50], Donor[50];
int i; // counter variables for the arrays such as Patient[i].BType... etc
int i1;
Run Code Online (Sandbox Code Playgroud)
那么函数的代码是这样的一行:
void compare(int &i, int &i1, Person &Patient[50], Person &Donor[50]);
Run Code Online (Sandbox Code Playgroud)
我试图通过i,i1,Patient和Donor结构.为什么这不起作用?有没有一种特殊的方法将这些结构传递给函数?
变量结构中的值也是从文件中读取的(不要认为这会改变任何内容).有任何想法吗?
我需要存储一个矩阵o N个学生有4个字段(数字,1年级,2年级和平均数).这可以通过结构来实现,但这不是本练习的目的.收集数据后(read_data),必须根据average(sort_matrix)进行排序.但是,当插入4个或更多学生时,会发生分段错误.我一直无法检测到问题的根源.我错过了什么?相关代码如下.
void read_data(float **mtx, int *size){
float num, grade1, grade2;
while( scanf("%f %f %f", &num, &grade1, &grade2)==3 ){
(*size)++;
mtx = (float**)realloc(mtx, (*size)*sizeof(float*) );
mtx[*size-1] = (float*)malloc(4*sizeof(float));
mtx[*size-1][0] = num;
mtx[*size-1][1] = grade1;
mtx[*size-1][2] = grade2;
mtx[*size-1][3] = (grade1+grade2)/2;
}
printf("Done reading\n");
}
void sort_matrix(float **mtx, int size){
int i=0, j=0;
float *aux = NULL;
for(i=0; i<size-1; i++){
for(j = 0; j<size-1-i; j++){
if(mtx[j][3] > mtx[j+1][3]){
aux = mtx[j];
mtx[j] = mtx[j+1];
mtx[j+1]=aux;
}
} …Run Code Online (Sandbox Code Playgroud)