如何通过指针的指针数组功能?

编程入门 行业动态 更新时间:2024-10-28 10:32:23
本文介绍了如何通过指针的指针数组功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道如何修改指针数组主,但不知道如何做到这一点时,我的功能需要修改它。**之间的code是我如何做到这一点的主要不使用的功能。我知道如何打印出的指针指针数组。我的问题是,假设我想将这些行**为()函数,我需要做什么修改?

code:

#包括LT&;&stdio.h中GT;#包括LT&;&string.h中GT;#包括LT&;&stdlib.h中GT;void函数(字符* []数组,INT大小);无效function_print(字符* []数组,INT大小);诠释的main(){    的char *数组[] = {0};    字符字[20];    ** scanf函数(%S字);    INT LEN = strlen的(字)+ 1;    数组[大小] =(字符*)malloc的(的sizeof(LEN));    strlcpy(数组【尺寸】,字的sizeof(数组[尺寸])); **    功能(数组,0);    返回0;}void函数(字符* []数组,INT大小){}无效function_print(字符* []数组,INT大小){   对于(INT X = 0; X<大小; X ++)   {       的printf(%S,*数组);       (阵列)++;   }}〜〜

解决方案

我做了一些修改,并意识到你的函数将这样做。这样下去,并宣读了修改如果您不知道它是如何工作的。

此外,我觉得有什么不对您的main()。首先,数组[大小] =(字符*)malloc的(的sizeof(LEN));往往因为没有大小这里定义报告错误。其次,如果你的大小意味着数组元素的号码,然后阵列[大小]会导致溢出。三,的malloc的参数应该是的sizeof(char)的* LEN,不是的sizeof(LEN),因为后者等于的sizeof(int)的。

你的函数改为

void函数(字符**,INT);

和按

叫它

函数(数组,0);

修改

我想通过修改你的意思是改变存储阵列中的指针。由于每个元素是的char * ,一个的char ** 将做的工作。

当你通过阵列作为参数,其实你传递的第一个元素在地址即可。然后在功能,您使用字符收到**粒子阵列。既然你也通过了大小,你可以使用它作为一个的char *粒子阵列[] ,每一个元素的地址是一模一样的有阵列在的main()。因此,你与粒子阵列做任何修改都将改变阵列。这是你想要的吗?

I know how to modify an array of pointers in main, but don't know how to do it when my function need to modify it. The code between ** is how I do it in main without using a function. I know how to print the array of pointer of pointer out. My question is, suppose I want to move these lines ** into function(), what do I need to modify?

code:

#include<stdio.h> #include<string.h> #include<stdlib.h> void function(char *array[], int size); void function_print(char *array[], int size); int main() { char *array[] = {0}; char word[20]; **scanf("%s", word); int len = strlen(word) + 1; array[size] = (char *)malloc(sizeof(len)); strlcpy(array[size], word, sizeof(array[size]));** function(array, 0); return 0; } void function(char *array[], int size) { } void function_print(char *array[], int size) { for(int x = 0; x < size; x ++) { printf("%s", *array); (array)++; } } ~ ~

解决方案

I made some edit and realized your function will do the same. So go on, and read the EDIT if you don't know how it works.

Besides I think there's something wrong in your main(). First, array[size] = (char *)malloc(sizeof(len)); tends to report an error because no size is defined here. Second, if by size you mean the number of elements in array, then array[size] would cause an overflow. Third, the argument of malloc should be sizeof(char)*len, not sizeof(len), since the latter equals sizeof(int).

Change your function to

void function(char **, int);

And call it by

function(array, 0);

EDIT

I think by "modify it" you mean to change the pointers stored in the array. Since every element is a char*, a char** will do the job.

When you pass array as an argument, actually you're passing the address of the first element. Then in the function you receive it with a char** parray. Since you have also passed the size, you can use it as a char* parray[], and the address of every element is exactly the same with array in main(). Thus, any modification you do with parray will change array. Is that what you want?

更多推荐

如何通过指针的指针数组功能?

本文发布于:2023-07-29 08:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1239119.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   数组   功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!