C 双指针对指针数组(**数组对*数组[])
我不清楚这两个之间有什么区别。我的教授写道**数组与*array[]相同,我们看到了一个他使用**数组的例子(因此,下课后我尝试将其与*array[]交换,但没有效果),谁能告诉我这两个数组是否与他所写的相同??无论如何,这门课是关于动态内存分配的 @我刚换了双指针,这行就开始抛出错误C 双指针对指针数组(**数组对*数组[]),c,pointers,multidimensional-array,dynamic-memory-allocation,C,Pointers,Multidimensional Array,Dynamic Memory Allocation,我不清楚这两个之间有什么区别。我的教授写道**数组与*array[]相同,我们看到了一个他使用**数组的例子(因此,下课后我尝试将其与*array[]交换,但没有效果),谁能告诉我这两个数组是否与他所写的相同??无论如何,这门课是关于动态内存分配的 @我刚换了双指针,这行就开始抛出错误 lines = malloc(sizeof(char*)); 还有一些内存正在重新分配 @是的,这是全部密码 对于下面的评论,没有,因为他的声明是 **array = *array[] 大更新
lines = malloc(sizeof(char*));
还有一些内存正在重新分配
@是的,这是全部密码
对于下面的评论,没有,因为他的声明是
**array = *array[]
大更新
我很抱歉给您带来不便,我只是在写这篇文章的时候太累了,这里是没有编辑的全部代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **lines; // global text buffer, organized as an array of lines
// --------------------------------------------------------------------------------
// initialize global buffer
void initialize()
{
lines = malloc(sizeof(char*));
lines[0] = NULL;
}
// --------------------------------------------------------------------------------
// return number of lines in buffer
int countLines()
{
int count = 0;
while(lines[count++]) ;
return count-1;
}
// --------------------------------------------------------------------------------
// print one line
void printLine(int line)
{
printf("Line %d: %p %p %s\n",line, &lines[line], lines[line], lines[line]);
}
// --------------------------------------------------------------------------------
// print all lines
void printAll()
{
int num_lines = countLines();
int line = 0;
printf("----- %d line(s) ----\n",num_lines);
while (line < num_lines)
printLine(line++);
printf("---------------------\n");
}
// --------------------------------------------------------------------------------
// free whole buffer
void freeAll()
{
int line = countLines();
while (line >= 0)
free(lines[line--]);
free(lines);
}
// --------------------------------------------------------------------------------
// insert a line before the line specified
void insertLine(int line, char *str)
{
int num_lines = countLines();
// increase lines size by one line pointer:
lines = realloc(lines, (num_lines+2) * sizeof(char*));
// move line pointers backwards:
memmove(&lines[line+1], &lines[line], (num_lines-line+1)*sizeof(char*));
// insert the new line:
lines[line] = malloc(strlen(str)+1);
strcpy(lines[line],str);
}
// --------------------------------------------------------------------------------
// remove the specified line
void removeLine(int line)
{
int num_lines = countLines();
// free the memory used by this line:
free(lines[line]);
// move line pointers forward:
memmove(&lines[line], &lines[line+1], (num_lines-line+1)*sizeof(char*));
// decrease lines size by one line pointer:
lines = realloc(lines, num_lines * sizeof(char*));
}
// --------------------------------------------------------------------------------
// insert a string into specified line at specified column
void insertString(int line, int col, char *str)
{
// make room for the new string:
lines[line] = realloc(lines[line], strlen(lines[line])+strlen(str)+1);
// move characters after col to the end:
memmove(lines[line]+col+strlen(str), lines[line]+col, strlen(lines[line])-col);
// insert string (without terminating 0-byte):
memmove(lines[line]+col, str, strlen(str));
}
// --------------------------------------------------------------------------------
// MAIN program
int main()
{
initialize();
printAll();
insertLine(0,"Das ist");
printAll();
insertLine(1,"Text");
printAll();
insertLine(1,"ein");
printAll();
insertLine(2,"kurzer");
printAll();
printf("lines[2][4] = %c\n",lines[2][4]);
insertString(2,0,"ziemlich ");
printAll();
removeLine(2);
printAll();
freeAll();
return 0;
}
#包括
#包括
#包括
字符**行;//全局文本缓冲区,组织为行数组
// --------------------------------------------------------------------------------
//初始化全局缓冲区
void initialize()
{
lines=malloc(sizeof(char*));
行[0]=NULL;
}
// --------------------------------------------------------------------------------
//返回缓冲区中的行数
int countLines()
{
整数计数=0;
而(行[count++]);
返回计数-1;
}
// --------------------------------------------------------------------------------
//打印一行
无效打印行(内部行)
{
printf(“行%d:%p%p%s\n”,行,&行[Line],行[Line],行[Line]);
}
// --------------------------------------------------------------------------------
//打印所有行
void printAll()
{
int num_lines=countLines();
内线=0;
printf(“----%d行----\n”,num\u行);
while(行数<行数)
printLine(line++);
printf(“--------------\n”);
}
// --------------------------------------------------------------------------------
//自由全缓冲区
void freeAll()
{
int line=countline();
而(行>=0)
自由(行[行--]);
免费(线路);
}
// --------------------------------------------------------------------------------
//在指定的行之前插入一行
void insertLine(int行,char*str)
{
int num_lines=countLines();
//将行大小增加一个行指针:
lines=realloc(lines,(num_lines+2)*sizeof(char*);
//向后移动行指针:
memmove(&lines[line+1],&lines[line],(num_lines-line+1)*sizeof(char*);
//插入新行:
行[line]=malloc(strlen(str)+1);
strcpy(行[line],str);
}
// --------------------------------------------------------------------------------
//删除指定的行
无效删除行(内部行)
{
int num_lines=countLines();
//释放此行使用的内存:
自由(行[行]);
//向前移动行指针:
memmove(&lines[line],&lines[line+1],(num_lines-line+1)*sizeof(char*);
//将行大小减少一行指针:
lines=realloc(行,num_行*sizeof(char*);
}
// --------------------------------------------------------------------------------
//在指定列的指定行中插入字符串
void insertString(int行、int列、char*str)
{
//为新字符串腾出空间:
行[line]=realloc(行[line],strlen(行[line])+strlen(str)+1;
//将列后的字符移动到末尾:
memmove(行[line]+col+strlen(str),行[line]+col,strlen(行[line])-col);
//插入字符串(不终止0字节):
memmove(行[line]+col、str、strlen(str));
}
// --------------------------------------------------------------------------------
//主程序
int main()
{
初始化();
printAll();
插入行(0,“Das ist”);
printAll();
插入行(1,“文本”);
printAll();
插入行(1,“ein”);
printAll();
插入行(2,“kurzer”);
printAll();
printf(“第[2][4]行=%c\n”,第[2][4]行];
插入字符串(2,0,“ziemlich”);
printAll();
去甲肾上腺素(2);
printAll();
freeAll();
返回0;
}
我的教授写道,**array
与*array[]
这在某些情况下是正确的,而在其他情况下则不正确
如果在函数中用作参数
void foo(int **array) {}
与
void foo(int *array[]) {}
int *array[];
当声明为变量时
int **array;
不一样
void foo(int *array[]) {}
int *array[];
如果你在问题中引用的代码是你的教授给你的,作为使用指针指向指针的指针数组的例子,我不确定这个类实际上会有多好。我怀疑这可能是作为调试练习提供的,也可能是您尝试的解决方案。无论如何,如果您只是在启用警告的情况下编译,那么在调试代码之前,您将发现许多需要注意的问题 关于您引用的代码,虽然您可以自由地使用全局文本缓冲区,但不使用全局缓冲区并根据需要传递指向数据的指针会更好地为您服务。有些实例、各种回调函数等需要全局数据,但根据经验,这些都是例外而不是规则 你的问题基本上归结为“如何正确使用指针数组和双指针(指针指向类型)变量。一个答案不可能完全涵盖这个主题,因为有太多的情况和上下文,其中一个可以(或应该)是使用和原因。然而,一些例子将有助于您理解基本的区别 从指向类型的指针数组开始(例如
char*ar
#define MAXL 128
#define MAXC 512
...
char **lines = NULL;
char buf[MAXC] = {0};
lines = malloc (MAXL * sizeof *lines);
size_t index = 0;
...
while (fgets (buf, MAXC, stdin)) {
lines[index++] = strdup (buf);
if (index == MAXL)
/* reallocate lines */
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
/* array is a static array of 4 pointers to char, initialized to the
4 string-literals that a part of the declaration */
char *array[] = { "The quick",
"brown fox",
"jumps over",
"the lazy dog." };
/* p is a pointer-to-pointer-to-char assigned the address of array */
char **p = array;
/* lines is a pointer-to-pointer-to-char initialized to NULL, used
below to allocate 8 pointers and storage to hold 2 copes of array */
char **lines = NULL;
size_t narray = sizeof array/sizeof *array;
size_t i;
printf ("\nprinting each string-literal at the address stored by\n"
"each pointer in the array of ponters named 'array':\n\n");
for (i = 0; i < narray; i++)
printf (" %s\n", array[i]);
printf ("\nprinting each string using a pointer to pointer to char 'p':\n\n");
for (i = 0; i < narray; i++, p++)
printf (" %s\n", *p);
p = array;
printf ("\nprinting each line using a pointer to pointer"
" to char 'p' with array notation:\n\n");
for (i = 0; i < narray; i++)
printf (" %s\n", p[i]);
/* allocate 8 pointers to char */
lines = malloc (2 * narray * sizeof *lines);
/* allocate memory and copy 1st 4-strings to lines (long way) */
for (i = 0; i < narray; i++) {
size_t len = strlen (array[i]);
lines[i] = malloc (len * sizeof **lines + 1);
strncpy (lines[i], array[i], len);
lines[i][len] = 0;
}
/* allocate memory and copy 1st 4-strings to lines
(using strdup - short way) */
// for (i = 0; i < narray; i++)
// lines[i] = strdup (array[i]);
/* allocate memory and copy again as last 4-strings in lines */
p = array;
for (i = 0; i < narray; i++, p++)
lines[i+4] = strdup (*p);
p = lines; /* p now points to lines instead of array */
printf ("\nprinting each allocated line in 'lines' using pointer 'p':\n\n");
for (i = 0; i < 2 * narray; i++)
printf (" %s\n", p[i]);
/* free allocated memory */
for (i = 0; i < 2 * narray; i++)
free (lines[i]);
free (lines);
return 0;
}