gpt4 book ai didi

C语言无法输出数组?

转载 作者:行者123 更新时间:2023-11-30 15:11:59 28 4
gpt4 key购买 nike

我似乎无法在 C 编程中正确输出字符串或整数数组。

我的问题是字符串存储在数组中,我只是无法将它们从数组中取出并输出。

当我手动输入位置(如Array[0])时,它会正确输出。

但是,最终循环无法输出我的任何数组条目。

为什么会发生这种情况?

预先感谢所有回复!

这是代码:

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

main (void)
{
int memory_start;
int memory_end;
int num_jobs;


printf("Enter the starting memory location to be used : ");
scanf("%d", &memory_start);

printf("\nEnter the ending memory location to be used : ");
scanf("%d", &memory_end);

printf("\nHow many jobs will be assigned to the memory locations? \n");
scanf("%d", &num_jobs);

int mem_size = memory_end - memory_start;
int mem_size_init = memory_end - memory_start;
char names[10][20];
int sizes[num_jobs];
int mem_st[10];
int mem_end[10];
int x;
int y;
int z;
int temp_size = 0;
int prev_temp_size;
char temp_name[20];


for(x=0; x<num_jobs; x++)
{
prev_temp_size = prev_temp_size + temp_size;
printf("\nEnter the size of the program (size > 0): ");
scanf("%d", &temp_size);

if( mem_size >= temp_size && temp_size > 0)
{
printf("\nEnter a program name (20 chars): %s", names[1]);
scanf("%s", &temp_name);

if(strlen(temp_name) <= 20)
{
strcpy(names[x], temp_name);
// names[x] = temp_name;
sizes[x] = temp_size;
mem_size = mem_size - temp_size;
mem_st[x] = prev_temp_size + memory_start;
mem_end[x] = temp_size + mem_st[x];
}
else
{
printf("\nProgram name is too long. Please re enter data.");
x--;
}
}
else
{
printf("\nMemory Size not supported (not enough space remaining).\nPlease re-enter data with a plausible memory request.");
printf("\nMemory size remaining: %d", mem_size);
x--;
}
}

printf("\nYour data is as follows: \n");
printf("\nYour initial memory size is: %d", mem_size_init);
printf("\nYour unused memory size is: %d", mem_size);

for(y=0; y<num_jobs; y++)
{
printf("Program: %s", names[y]);
printf("\n", names[y], " memory size is as follows: \n", "memory start: %d", mem_st[y], " memory end: %d", mem_end[y]);
}
}

最佳答案

这是更正后的代码:

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

main (void)
{
int memory_start;
int memory_end;
int num_jobs;

printf("Enter the starting memory location to be used : ");
scanf("%d", &memory_start);

printf("\nEnter the ending memory location to be used : ");
scanf("%d", &memory_end);

printf("\nHow many jobs will be assigned to the memory locations? ");
scanf("%d", &num_jobs);

int mem_size = memory_end - memory_start;
int mem_size_init = memory_end - memory_start;
char names[10][20];
int sizes[num_jobs];
int mem_st[10];
int mem_end[10];
int x;
int y;
int z;
int temp_size = 0;
int prev_temp_size;
char temp_name[20];

for(x=0; x<num_jobs; x++)
{
prev_temp_size = prev_temp_size + temp_size;
printf("\nEnter the size of the program (size > 0): ");
scanf("%d", &temp_size);

if( mem_size >= temp_size && temp_size > 0)
{
printf("\nEnter a program name (20 chars): ");
scanf("%s", temp_name);
getchar();

if(strlen(temp_name) <= 20)
{
strcpy(names[x], temp_name);
sizes[x] = temp_size;
mem_size = mem_size - temp_size;
mem_st[x] = prev_temp_size + memory_start;
mem_end[x] = temp_size + mem_st[x];
}
else
{
printf("\nProgram name is too long. Please re enter data.");
x--;
}
}
else
{
printf("\nMemory Size not supported (not enough space remaining).\nPlease re-enter data with a plausible memory request.");
printf("\nMemory size remaining: %d", mem_size);
x--;
}
}

printf("\nYour data is as follows: \n");
printf("\nYour initial memory size is: %d", mem_size_init);
printf("\nYour unused memory size is: %d\n", mem_size);

for(y=0; y<num_jobs; y++)
{
printf("\nProgram: %s\n", names[y]);
printf("\nmemory size is as follows:\nmemory start: %d memory end: %d\n", mem_st[y], mem_end[y]);
}
}

除了输出格式的变化之外,主要变化是:

  • 删除了 scanf("%s", &temp_name); 中的 &,因为数组的名称已经返回对其第一个元素的引用。

  • scanf("%s", temp_name); 之后添加了 getchar();,因为读入字符串后会出现额外的 \n 保留在流中,这会导致下一个 scanf 失败。

  • 这个:

    printf("\n", names[y], " memory size is as follows: \n", "memory start: %d", mem_st[y], "    memory end: %d", mem_end[y]);    

    这不是格式说明符的准备方式,所有变量都需要放在最后,在单个格式说明符字符串之后。

关于C语言无法输出数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35306833/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com