动态数组int打印问题 - 也许不是有效的内存单元格
#include <stdio.h>
#include <stdlib.h>
int find_lenght(int *arrr){
int i = 0;
while(arrr[i] != '\0'){
i++;
}
return i;
}
void init_array(int *arrr){
arrr=(int*)malloc(1*sizeof(int));
printf("New element:");
int lenght = find_lenght(arrr);
scanf("%d", &arrr[lenght]);
printf("Lenght = %d\n",lenght);
printf("Array elements are:\n");
for(int i = 0; i <= lenght; i++) {
printf("%d,", arrr[i]);
}
}
void print_array(int *arrr){
printf("Array elements are:\n");
int lenght = find_lenght(arrr);
for(int i = 0; i == lenght; i++) {
printf("%d,", arrr[i]);
}
}
int main() {
int *arr = NULL;
init_array(arr);
print_array(arr);
}
我不知道我在这里想念什么。 我的观点是填写然后打印动态阵列,
我所教的是它没有填充应有的方式,因此没有任何打印。
#include <stdio.h>
#include <stdlib.h>
int find_lenght(int *arrr){
int i = 0;
while(arrr[i] != '\0'){
i++;
}
return i;
}
void init_array(int *arrr){
arrr=(int*)malloc(1*sizeof(int));
printf("New element:");
int lenght = find_lenght(arrr);
scanf("%d", &arrr[lenght]);
printf("Lenght = %d\n",lenght);
printf("Array elements are:\n");
for(int i = 0; i <= lenght; i++) {
printf("%d,", arrr[i]);
}
}
void print_array(int *arrr){
printf("Array elements are:\n");
int lenght = find_lenght(arrr);
for(int i = 0; i == lenght; i++) {
printf("%d,", arrr[i]);
}
}
int main() {
int *arr = NULL;
init_array(arr);
print_array(arr);
}
I don't know what am i missing here.
My point is to fill in and then print dynamic array
Also my taught is it's not filling the way it should, so it hasn't anything to print.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的
ARR
MAIM中的指针永远不会分配,因为您的init_array
将分配的内存(malloc
的返回值)分配给输入参数arrr
,这是一个本地变量。您有两种解决方案可以正确实现您想做的事情。第一个(在我的角度看,越好),通过使您的
init_array
返回要分配的分配的内存地址:另一种方法是使您的
init_ init_array
指向指针的指针,因此该功能可以分配此指针:Your
arr
pointer in main is never assigned because yourinit_array
assign the address of the allocated memory (the return value ofmalloc
) to the input parameterarrr
, which is, a local variable.You have mainly two solutions to properly achieve what you want to do. The first one (the better one in my point of view), by making your
init_array
returning the allocated memory address to be assigned:Another way is to make your
init_array
function taking a pointer to a pointer, so the function can assign this pointer:您需要将指针转到
int
以更改传递指针。对于循环,您的在打印功能中无效。您还需要自己设置前哨价值。
https://godbolt.org/z/drkej3kt5
You need to pass the pointer to pointer to
int
to change passed pointer. Yourfor
loop is invalid in print function. You need also to set the sentinel value yourself.https://godbolt.org/z/drKej3KT5
主函数上的数组仍然为空。更好的方法是在初始化之后调用print_array()函数。您只需将print_array(arrr)放入init_array()之后,然后在for loop语句之后。
the array on your main function is still NULL. the better way to do is just call the print_array() function after you initialize it. you just simply put print_array(arrr) inside init_array() and after the for loop statement.
该行
可能会调用未定义的行为,因为
find_length
要求其参数是指向null终止的int
数组的第一个元素的指针。但是,由arrr
指向的内存内容是不确定的,因为尚未初始化。因此,不能保证终止终止。The line
may invoke undefined behavior, because
find_length
requires its argument to be a pointer to the first element of a null-terminatedint
array. However, the content of the memory pointed to byarrr
is indeterminate, because it has not been initialized. Therefore, it is not guaranteed to be null-terminated.