简体   繁体   中英

How can I make scanf and printf functions of an array in C?

I am trying to make two separate functions, one for a reading of an array, and another one for prining it. My code looks something like this:

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

void read(int n, int v[100])
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&v[i]);
    }
}

void print(int n, int v[100])
{
    for(int i=0;i<n;i++)
    {
        printf("%d",v[i]);
    }
}

int main()
{
    int v[100];
    int n;
    read(n,v);
    print(n,v);
}

But if i read something like this

5
1
2
3
4
5

it prints this:

12345167773430408951321408978481140419686004089785612740906704021677734340894

and other numbers. Any suggestions?

Here is a program that does what you want it do to.

#include <stdio.h>

// Function Prototypes Start
void array_reader(int array_length, int array[]);
void array_printer(int array_length, int array[]);
// Function Prototypes End


int main(void) {

    int max_amt_to_scan;
    printf("How many numbers would you like to scan in?\n");
    scanf("%d", &max_amt_to_scan);

    int num_array[max_amt_to_scan];

    printf("Please enter %d numbers!\n", max_amt_to_scan);
    array_reader(max_amt_to_scan, num_array);

    printf("The numbers you entered were:\n");
    array_printer(max_amt_to_scan, num_array);

    return 0;
}


// Scans or reads in numbers, into an array
void array_reader(int array_length, int array[]) {

    int i = 0;
    while (i < array_length) {

        scanf("%d", &array[i]);
        i++;
    }
}


// Prints out an array of numbers
void array_printer(int array_length, int array[]) {

    int i = 0;
    while (i < array_length) {

        if (i == (array_length - 1)) {
            printf("%d\n", array[i]);
        } else {
            printf("%d, ", array[i]);
        }
            i++;
    }
}

You don't initialize n in main . The n in main and the n in read are different variables, so changing n in read doesn't affect the n in main , which remains uninitialized and hence you have an undefined behaviour in print .

You have to either pass n as a pointer or read must return n :

void read(int &n, int v[100])
{
    scanf("%d",n);
    for(int i=0;i<*n;i++)
    {
        scanf("%d",&v[i]);
    }
}

int main()
{
    int v[100];
    int n;
    read(&n,v);
    print(n,v);
}

or

int read(int v[100])
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&v[i]);
    }
    return n;
}

int main()
{
    int v[100];
    int n = read(v);
    print(n,v);
}

This is what it should look like:

void read(int *array, int size)
{
    printf("Now enter %d values:\n", size);
    for(int i = 0; i < size; i++)
    {
        scanf("%d", array + i);
        /* array + i = &(array[i]) */
    }
}

void print(int *array, int size)
{
    printf("Here is your array:\n");
    for(int i = 0; i < size; i++)
    {
        printf("%d%c", array[i], i == size - 1 ? '\n' : ' ');
        /* ternary puts spaces and \n at the end */
    }
}

int main()
{
    int size;
    printf("Enter array size :\n");
    scanf("%d", &size);
    int array[size];  /* Keep one array */
    read(array, size);
    print(array, size);
}

Ok, i solved it by declaring the variable n and v[100] globally. Thanks.

EDIT

I also tried to use the pointer and it looks something like this:

#include<stdio.h>
void read(int a[], int* n)
{
    scanf("%d",n);
    for(int i=0;i<*n;i++)
    {
        scanf("%d",&a[i]);
    }
}
void print(int a[], int n)
{
    for(int i=0;i<n;i++)
        printf("%d ",a[i]);

}
void main()
{
    int a[20],n;
    read(a,&n);
    print(a,n);
}

And it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM