-1

Sorry for that title. I really didn't know how to define this problem.

I was needed to declare integer array of N numbers and to fill it with random nums in void function. Then that array needs to be printed in main. The thing is that i am not allowed to use printf in void function so only way to print in main is to use pointers I guess. My knowledge is limited as I am beginner at pointers. Thx in advance and sorry for bad english.

Here is my code so far. When I compile it marks segmentation error.

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

void form();

int main()
{

    int N, a[100];

    printf("Input index: \n");
    scanf("%d", &N);

    form(N, &a);

    printf("Array: \n");

    for (int i = 0; i < N; i++) {
        printf("a[%d] = %d", i, a[i]);
    }

}


void form(int N, int *ptr[100])
{

    srand(time(NULL));

    for (int i = 0; i < N; i++) {
        *ptr[i] = rand() % 46;

    }
Misery
  • 3
  • 2
  • Your compiler should yell at you. Use a correct prototype, your code is not standard compliant and invokes undefined behaviour. (And that is much more fundamental than pointers. Did you skip the chapter about functions and function declarations in your C book?) – too honest for this site Nov 17 '16 at 19:59
  • you can use `printf` in any function you want. – user3528438 Nov 17 '16 at 20:00
  • user3528438 This is school exercise and professor told me not to use printf. @Olaf Is this prototype correct: void form(int, int). The function declaration seems fine. That is the way I was taught. – Misery Nov 17 '16 at 20:11
  • @Misery: Please recap how functions are declared! No offence, but I honestly have the impression you have not understood how functions are declared and how they work. This is **vital** knowlege (not only in C, btw)! – too honest for this site Nov 17 '16 at 20:18
  • `int *ptr[100]`, this is wrong. `int (*ptr)[100]` – user3528438 Nov 17 '16 at 20:33
  • Thanks for answers. @Olaf I will learn more carefully next time. – Misery Nov 17 '16 at 20:44
  • @user3528438: 1) That's the second problem, but your approach is the exactly wrong direction. There is a cannonical solution for this (which is in every C book). 2) The first problem what I wrote. – too honest for this site Nov 17 '16 at 20:50

3 Answers3

0

So a couple things:

void form();

As Olaf was alluding to, this declaration is incorrect - you are missing the applicable parameters. Instead, it should be

void form(int N, int ptr[100]);

The main reason your program is crashing is because of the following line:

*ptr[i] = rand() % 46;

You are dereferencing the pointer at i, which is actaully giving you a number - what you want is to assign the value of the pointer at i the new random value:

ptr[i] = rand() % 46;

As related reading, see this question about passing an array in as a function parameter (basically, int ptr[] is the same thing as int * ptr)

Community
  • 1
  • 1
schil227
  • 292
  • 3
  • 11
0

There are several issues in your code.

1) Your array decalaration form() is obsolete. Use proper prototype.

2) For declaring a VLA, declare it after reading N instead of using a fixed size array.

3) An array gets converted into a pointer to its first element when passed to a function. See: What is array decaying?


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

void form(int, int*); /* see (1) */

int main(void) /* Standard complaint prototype for main. 
                 If you need to pass arguments you can use argc, and argv */
{
    int N;

    printf("Input size: \n");
    scanf("%d", &N);

    int a[N];   /* see (2) */
    form(N, a); /* see (3) */
    printf("Array: \n");

    for (int i = 0; i < N; i++) {
        printf("a[%d] = %d", i, a[i]);
    }
}


void form(int N, int *ptr) { /* Modified to match the prototype
    srand(time(NULL));

    for (int i = 0; i < N; i++) {
        ptr[i] = rand() % 46;

    }
}
Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
-1

Small modifications on your code:

1) Correction and simplification of parameter handling at function call. Just hand over "a", it's an array, so it is an address, you can use int *ptr, or int ptr[], or int ptr[100] in the formal parameter list for it. So you can use simply ptr[i] in your function.

2) Make a prototype for function from old-style declaration providing parameter list.

3) int i; declaration before the for loop - not mandatory, depends on your compiler standard

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

void form(int N, int *ptr);

int main()
{

    int N, a[100];

    printf("Input index: \n");
    scanf("%d", &N);

    form(N, a);

    printf("Array: \n");
    int i;
    for (i = 0; i < N; i++) {
        printf("a[%d] = %d", i, a[i]);
    }

}


void form(int N, int *ptr)
{

    srand(time(NULL));
    int i;
    for (i = 0; i < N; i++) {
        ptr[i] = rand() % 46;

    }
}
quantummind
  • 2,086
  • 1
  • 14
  • 20