无法通过函数向数组添加项目 (C)
Having trouble adding an item to an array through a function (C)
所以我是编程新手,有时我会有点乱,所以请多多包涵。
我正在尝试制作一个处理两个不同数组的程序,并且我试图制作一个向其中一个添加项目的函数,但是我遇到了一些麻烦,该程序如下所示:
#include <stdio.h>
#include <stdlib.h>
void insert(int **queue, int *cont, int *item); //the idea is to
// call the array i want to add the item to,
// the counter for that respective array, and the scanned
// item to be added
void instructions();
int main(int argc, char *argv[]) {
int i=0, j=0; //counters for each array
int value; //scanned value to insert
int op;
int *queue1[10], *queue2[10]; //the arrays i'm going to use
while (op!=0){
instructions();
scanf("%d", &op);
switch(op) {
case 1:
printf("You've chosen to enqueue an item, please insert the item you want to add to the array:\n" );
scanf("%d", &value);
insert(queue1,&i,&value); //here's the case for adding
// an item to array one, i called the array, its
//counter and the value i want to insert
break;
default :
if (op!=0)
printf("Invalid Option.\n" );
}
}
return 0;
}
void insert(int **queue, int *count, int *item){
if(*count==9){
printf("You can't add more items.");
}else{
*queue[*count]=item;
*count=(*count+1);
}
}
void instructions(){
printf("What function do you want to use?\n1)Insert Queue 1.\n2)Remove Queue 1.\n3)Print Queue 1.\n4)Insert Queue 2.\n5)Remove Queue 2.\n6)Insert Queue 2.\n0)Exit.\n");
}
如您所见,该程序是一团乱七八糟的指针之类的东西,我到处查看,阅读了有关函数的页面和页面,但是在应用了我想要的东西之后,我找不到任何东西来帮助我实现我想要的东西以为我已经学会了,这就是我最终遇到的烂摊子。计数器似乎工作正常并且每次使用函数时都会增加,这正是我想要的,但是当我尝试在指令函数之后放置 printf("%d\n", queue1[0]);
以显示该值是否确实已插入数组时,它显示了一些非常奇怪的数字,在我看来甚至都不像地址,最重要的是,在使用函数 3 次到 [=24 之后=]随机数到数组,程序就崩溃了。
因此,如果有人可以帮助我,一个完全的菜鸟,了解我做错了什么,这样我就可以解决这个问题,我将不胜感激。
您根本没有为 "queue" 元素分配任何内存。
insert(queue1,&i,&value);
由于 i
和 value
是 int
类型的局部变量,当您在数组中填充指针时,您将一次又一次地使用相同的内存位置。
void insert(int **queue, int *count, int *item){
if(*count==9){
printf("You can't add more items.");
}else{
*queue[*count]=item;
*count=(*count+1);
}
}
在此函数中,您从调用者处接收到局部变量的地址。
当您将 item
分配给数组的一个元素时,它将指向来自调用函数的 value
。
您将使用相同的地址填充数组的 每个 元素。
另一个问题:
queue1
未初始化,包含 10 个随机地址。
*queue[*count]=item;
在这里取消引用那些导致崩溃的地址。
您需要为每个新元素分配内存。
int *queue1[10];
...
insert(queue1, &i, value);
...
void insert(int **arr, int *count, int value){
if(*count==9){
printf("You can't add more items.");
}else{
int *item = malloc(sizeof(*item));
if (item != NULL)
{
*item = value;
arr[*count]=item;
*count=(*count+1);
}
else
{
printf("memory allocation error\n");
}
}
}
或者...
当你使用数组时,你可以直接存储值而不需要任何分配
int queue1[10];
...
insert(queue1, &i, value);
...
void insert(int *arr, int *count, int item){
if(*count==9){
printf("You can't add more items.");
}else{
arr[*count]=item;
*count=(*count+1);
}
}
所以我是编程新手,有时我会有点乱,所以请多多包涵。
我正在尝试制作一个处理两个不同数组的程序,并且我试图制作一个向其中一个添加项目的函数,但是我遇到了一些麻烦,该程序如下所示:
#include <stdio.h>
#include <stdlib.h>
void insert(int **queue, int *cont, int *item); //the idea is to
// call the array i want to add the item to,
// the counter for that respective array, and the scanned
// item to be added
void instructions();
int main(int argc, char *argv[]) {
int i=0, j=0; //counters for each array
int value; //scanned value to insert
int op;
int *queue1[10], *queue2[10]; //the arrays i'm going to use
while (op!=0){
instructions();
scanf("%d", &op);
switch(op) {
case 1:
printf("You've chosen to enqueue an item, please insert the item you want to add to the array:\n" );
scanf("%d", &value);
insert(queue1,&i,&value); //here's the case for adding
// an item to array one, i called the array, its
//counter and the value i want to insert
break;
default :
if (op!=0)
printf("Invalid Option.\n" );
}
}
return 0;
}
void insert(int **queue, int *count, int *item){
if(*count==9){
printf("You can't add more items.");
}else{
*queue[*count]=item;
*count=(*count+1);
}
}
void instructions(){
printf("What function do you want to use?\n1)Insert Queue 1.\n2)Remove Queue 1.\n3)Print Queue 1.\n4)Insert Queue 2.\n5)Remove Queue 2.\n6)Insert Queue 2.\n0)Exit.\n");
}
如您所见,该程序是一团乱七八糟的指针之类的东西,我到处查看,阅读了有关函数的页面和页面,但是在应用了我想要的东西之后,我找不到任何东西来帮助我实现我想要的东西以为我已经学会了,这就是我最终遇到的烂摊子。计数器似乎工作正常并且每次使用函数时都会增加,这正是我想要的,但是当我尝试在指令函数之后放置 printf("%d\n", queue1[0]);
以显示该值是否确实已插入数组时,它显示了一些非常奇怪的数字,在我看来甚至都不像地址,最重要的是,在使用函数 3 次到 [=24 之后=]随机数到数组,程序就崩溃了。
因此,如果有人可以帮助我,一个完全的菜鸟,了解我做错了什么,这样我就可以解决这个问题,我将不胜感激。
您根本没有为 "queue" 元素分配任何内存。
insert(queue1,&i,&value);
由于 i
和 value
是 int
类型的局部变量,当您在数组中填充指针时,您将一次又一次地使用相同的内存位置。
void insert(int **queue, int *count, int *item){
if(*count==9){
printf("You can't add more items.");
}else{
*queue[*count]=item;
*count=(*count+1);
}
}
在此函数中,您从调用者处接收到局部变量的地址。
当您将 item
分配给数组的一个元素时,它将指向来自调用函数的 value
。
您将使用相同的地址填充数组的 每个 元素。
另一个问题:
queue1
未初始化,包含 10 个随机地址。
*queue[*count]=item;
在这里取消引用那些导致崩溃的地址。
您需要为每个新元素分配内存。
int *queue1[10];
...
insert(queue1, &i, value);
...
void insert(int **arr, int *count, int value){
if(*count==9){
printf("You can't add more items.");
}else{
int *item = malloc(sizeof(*item));
if (item != NULL)
{
*item = value;
arr[*count]=item;
*count=(*count+1);
}
else
{
printf("memory allocation error\n");
}
}
}
或者...
当你使用数组时,你可以直接存储值而不需要任何分配
int queue1[10];
...
insert(queue1, &i, value);
...
void insert(int *arr, int *count, int item){
if(*count==9){
printf("You can't add more items.");
}else{
arr[*count]=item;
*count=(*count+1);
}
}