有关 scanf 和内存分配的帮助

发布于 2024-10-19 18:37:41 字数 1362 浏览 5 评论 0原文

这是我的代码:

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

#define MAX 50

float** getdata(void);
void calculations(float* fPtr);
//void printoriginal(float* values, int inputnum, float* fPtr);

int main(void)
{
    float** fPtr; 
    float* values; 

    fPtr = getdata();
    calculations(*fPtr);
    int element;
//  printoriginal(*values, inputnum, *fPtr);



    system("PAUSE");
    return 0;
}

float getvalues(void)
{
     float* values = (float*)calloc(*inputnum, sizeof(float));

float** getdata(void)
{
    int i;
    int n;
    float** fPtr;
    int* inputnum;

    printf("How many values do you want to input into the array?");
    scanf("%d", inputnum);

    float* values = (float*)calloc(*inputnum, sizeof(float));

    if (values == NULL)
    {   printf("Memory overflow\n");
        exit(101);
    }


    for(i = 0; i < *inputnum; i++)
    {
        n = i + 1;
        printf("Please enter your %d number: ", n);
        scanf("%f",(values+i));
    }


    fPtr = (float**)calloc(*inputnum+1, sizeof(float*));
    if (fPtr == NULL)
    {   printf("Memory overflow\n");
        exit(101);
    }

    for(int c=0; c < *inputnum; c++)
    {
        *(fPtr+i) = (values+i);
    }
printf("%f", values+2);

return fPtr;
}

我扫描 getdata 中的值,但我正在扫描垃圾。我很确定我的 scanf 已关闭。另外,我如何能够通过引用传回我的值数组?我对此也感到非常困难。谢谢大家。

Here is my code:

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

#define MAX 50

float** getdata(void);
void calculations(float* fPtr);
//void printoriginal(float* values, int inputnum, float* fPtr);

int main(void)
{
    float** fPtr; 
    float* values; 

    fPtr = getdata();
    calculations(*fPtr);
    int element;
//  printoriginal(*values, inputnum, *fPtr);



    system("PAUSE");
    return 0;
}

float getvalues(void)
{
     float* values = (float*)calloc(*inputnum, sizeof(float));

float** getdata(void)
{
    int i;
    int n;
    float** fPtr;
    int* inputnum;

    printf("How many values do you want to input into the array?");
    scanf("%d", inputnum);

    float* values = (float*)calloc(*inputnum, sizeof(float));

    if (values == NULL)
    {   printf("Memory overflow\n");
        exit(101);
    }


    for(i = 0; i < *inputnum; i++)
    {
        n = i + 1;
        printf("Please enter your %d number: ", n);
        scanf("%f",(values+i));
    }


    fPtr = (float**)calloc(*inputnum+1, sizeof(float*));
    if (fPtr == NULL)
    {   printf("Memory overflow\n");
        exit(101);
    }

    for(int c=0; c < *inputnum; c++)
    {
        *(fPtr+i) = (values+i);
    }
printf("%f", values+2);

return fPtr;
}

I scanf the values in getdata, but I am scanning in garbage. Im pretty certain my scanf is off. Also, how would I be able to pass back my values array through reference?? I am having a very hard time with that as well. Thanks everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

末骤雨初歇 2024-10-26 18:37:41

而不是

int * inputnum;
scanf( ..., inputnum );

你想要的:

int inputnum;
scanf( ..., &inputnum );

要么这样,要么你需要插入 inputnum: 的分配,

/* Very non-C-worthy code sample */
int * inputnum;
inputnum = malloc( sizeof *inputnum );
if( inputnum == NULL ) {
    perror( "malloc" );
    exit( EXIT_FAILURE );
}
scanf( ..., inputnum );

但这对于熟悉 C 的人来说看起来很奇怪
(除非有其他原因,在此
情况似乎没有任何原因。)

您遇到的问题是 scanf 是
将接收到的值存储在指向的位置
通过 inputnum,这是一个虚假位置,因为 inputnum
未初始化。

Instead of

int * inputnum;
scanf( ..., inputnum );

you want:

int inputnum;
scanf( ..., &inputnum );

Either that, or you need to insert an allocation of inputnum:

/* Very non-C-worthy code sample */
int * inputnum;
inputnum = malloc( sizeof *inputnum );
if( inputnum == NULL ) {
    perror( "malloc" );
    exit( EXIT_FAILURE );
}
scanf( ..., inputnum );

but that will look very strange to anyone familiar with C
(Unless there is some other reason for it, and in this
case there does not appear to be any reason.)

The problem you are experiencing is that the scanf is
storing the received value in the location pointed to
by inputnum, which is a bogus location since inputnum
is uninitialized.

莫相离 2024-10-26 18:37:41

我喜欢将对象“分配”给一个函数。在您的程序中,您的值在函数 getdata() 中创建,在 calculations() 中使用,并且从未释放。

在我看来,如果 main() 函数负责它们,那么编码会更容易。 在上面的代码片段

int main(void) {
    int inputnum, innum;
    double *fPtr;

    printf("How many values do you want to input into the array?");
    fflush(stdout);
    if (scanf("%d", &inputnum) != 1) {
        fprintf(stderr, "Error in input. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    if (inputnum > MAX) { /* ... nothing yet ... */ }

    fPtr = calloc(inputnum, sizeof *fPtr);
    if (fPtr == NULL) {
        fprintf(stderr, "Not enough memory. Program aborted.\n");
        exit(EXIT_FAILURE);
    }

    /* use fPtr in other functions */
    innum = data_get(fPtr, inputnum);
    data_print(fPtr, innum);

    free(fPtr);
    return 0;
}

中,我使用了函数 data_get()data_print() ,其原型与您的原型略有不同。这些函数采用一个指针和多个元素。

int data_get(double *data, int num);
void data_print(double *data, int num);

第一个将最多 num 个双精度数读取到 data 指向的内存中,并返回有效输入的双精度数。
第二个需要打印双精度数。

哦!除非您有充分的理由使用浮动,否则不要使用它们。总是更喜欢double

I like to have objects "assigned" to one single function. In your program, your values are created in the function getdata(), used in calculations() and never released.

If the main() function was responsible for them, it would make coding easier, in my opinion. Something like

int main(void) {
    int inputnum, innum;
    double *fPtr;

    printf("How many values do you want to input into the array?");
    fflush(stdout);
    if (scanf("%d", &inputnum) != 1) {
        fprintf(stderr, "Error in input. Program aborted.\n");
        exit(EXIT_FAILURE);
    }
    if (inputnum > MAX) { /* ... nothing yet ... */ }

    fPtr = calloc(inputnum, sizeof *fPtr);
    if (fPtr == NULL) {
        fprintf(stderr, "Not enough memory. Program aborted.\n");
        exit(EXIT_FAILURE);
    }

    /* use fPtr in other functions */
    innum = data_get(fPtr, inputnum);
    data_print(fPtr, innum);

    free(fPtr);
    return 0;
}

In the snippet above, I used the functions data_get() and data_print() with a somewhat different prototype than you have. These functions take a pointer and a number of elements.

int data_get(double *data, int num);
void data_print(double *data, int num);

The first one reads up to num doubles into the memory pointed to by data and returns the number of doubles that was effectively entered.
The second takes number of doubles to print.

Oh! Unless you have a very good reason to use floats, don't use them. Always prefer doubles.

玉环 2024-10-26 18:37:41

也可以代替

printf("%f",values+2);

使用

printf("%f", *(值+2));

Also Instead of

printf("%f", values+2);

use

printf("%f", *(values+2));

挥剑断情 2024-10-26 18:37:41

有很多错误,我不知道从哪里开始:)

下面完全重写了代码。请研究一下这个。

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

#define MAX 50

int  get_data        (float** array);           /* must be pointer-to-pointer to return through parameter */
void print_data      (const float* array, int size); /* print data */
void do_weird_things (float* array, int size);  /* do weird things with the data */
void clear_data      (float*  array);           /* clean up */

int main(void)
{
    float* array;
    int size;

    size = get_data (&array);

    printf("Before weird things:\n");
    print_data (array, size);

    do_weird_things (array, size);

    printf("After weird things:\n");
    print_data (array, size);

    clear_data (array);


    system("PAUSE");
    return 0;
}

int get_data (float** array)
{
    int i;
    int items;

    printf("How many values do you want to input into the array? ");
    scanf("%d", &items);
    getchar(); /* remove \n from stdin */

    *array = calloc(items, sizeof(float));

    if (*array == NULL)
    {
        printf("Memory overflow\n");
        exit(EXIT_FAILURE);
    }


    for(i = 0; i < items; i++)
    {
        printf("Please enter your %d number: ", i+1);
        scanf("%f", (*array) + i);
        getchar(); /* remove \n from stdin */
    }

    return items;
}

void do_weird_things (float* array, int size) /* do whatever data manipulation you wish here */
{
  int i;

  for(i=0; i<size; i++)
  {
    array[i] += i;
  }
}

void print_data (const float* array, int size)
{
  int i;

  for(i=0; i<size; i++)
  {
    printf("%f\t", array[i]);
  }
  printf("\n");
}

void clear_data      (float*  array)
{
  free(array);
}

There is so many errors, I didn't know where to start :)

Code completely rewritten below. Please study this.

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

#define MAX 50

int  get_data        (float** array);           /* must be pointer-to-pointer to return through parameter */
void print_data      (const float* array, int size); /* print data */
void do_weird_things (float* array, int size);  /* do weird things with the data */
void clear_data      (float*  array);           /* clean up */

int main(void)
{
    float* array;
    int size;

    size = get_data (&array);

    printf("Before weird things:\n");
    print_data (array, size);

    do_weird_things (array, size);

    printf("After weird things:\n");
    print_data (array, size);

    clear_data (array);


    system("PAUSE");
    return 0;
}

int get_data (float** array)
{
    int i;
    int items;

    printf("How many values do you want to input into the array? ");
    scanf("%d", &items);
    getchar(); /* remove \n from stdin */

    *array = calloc(items, sizeof(float));

    if (*array == NULL)
    {
        printf("Memory overflow\n");
        exit(EXIT_FAILURE);
    }


    for(i = 0; i < items; i++)
    {
        printf("Please enter your %d number: ", i+1);
        scanf("%f", (*array) + i);
        getchar(); /* remove \n from stdin */
    }

    return items;
}

void do_weird_things (float* array, int size) /* do whatever data manipulation you wish here */
{
  int i;

  for(i=0; i<size; i++)
  {
    array[i] += i;
  }
}

void print_data (const float* array, int size)
{
  int i;

  for(i=0; i<size; i++)
  {
    printf("%f\t", array[i]);
  }
  printf("\n");
}

void clear_data      (float*  array)
{
  free(array);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文