0

I'm having some problem with my code, it crashes when I enter the value of n.

I have entered what I think the code should do.

I guess there is an issue with the pointer to *a[i] which cause the program to crash.

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


void assign_zero(int * a[], int * n){ // fetches value of a and n
    int i;
    for (i = 0; i < *n; i++)
        *(a)[i] = 0; // puts every value in the array to 0 (a[3] = {0,0,0,0})
}

int main(){
    int n;

    printf("Choose value of n: ");
    scanf("%i", &n); // stores the int at n's adress
    int a[n]; // sets the length of the array

    assign_zero(&a, &n); // sends the adress of a and n to assign_zero

    int x;
    for (x = 0; x < n; x++)
    printf("%i", a[x]); // prints 00000... depending of n's value

    return 0;
}
4
  • 1
    Crank up your compiler warnings. The first argument you feed to assign_zero is of the wrong type. Commented Jun 30, 2015 at 13:40
  • Why are you passing the addresses? Commented Jun 30, 2015 at 13:40
  • I am new to C but as I understand it I must pass the adress to change the variable in assign_zero since it's a void and wont return a value. Commented Jun 30, 2015 at 13:43
  • 1
    @DavidCarlsson Yes, but what you're passing is already a pointer. You can't change the pointer itself, but you can change the content of the memory location it points to. Commented Jun 30, 2015 at 13:44

3 Answers 3

3

In your call, you're using

 assign_zero(&a, &n);

call. But the function signature is

void assign_zero(int * a[], int * n)

which seems wrong in this case. You only need a pointer, to hold the passed array, like

 void assign_zero(int * a, int * n)

will suffice. Then, in the function, you can directly use a[i] to access the elements of the array.

That said, it seems you're not modifying the value of n from assign_zero() function. If that is the case, you don't need to pass it as a pointer.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! How easy and clear it can be when someone shows you. Big ups to all of you!
Better late than never :)
2

Too many *'s! Try this...

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


void assign_zero(int * a, int n){ // fetches value of a and n
    int i;
    for (i = 0; i < n; i++)
        a[i] = 0; // puts every value in the array to 0 (a[3] = {0,0,0})
}

int main(){
    int n;

    printf("Choose value of n: ");
    scanf("%i", &n); // stores the int at n's adress
    int a[n]; // sets the length of the array

    assign_zero(a, n); // sends the address of a and n to assign_zero

    int x;
    for (x = 0; x < n; x++)
    printf("%i", a[x]); // prints 00000... depending of n's value

    return 0;
}

You don't need to pass the address of n into assign_zero as you don't modify it within the function.

And any pointer is implicitly (and possibly dangerously) the base address of an array. That is to say you can index any pointer with square brackets, therefore you only need a to be declared a pointer to int.

5 Comments

Edited with more info
Because you can happily index a pointer out of bounds and good luck debugging the weird effects that can cause...
Oh that doesn't mean it's dangerous, only if you are not careful. Driving is dangerous too, but how often do you have an accident? And there are drivers that often have accidents because they are not careful. Or a chef might be dumb enough to cut his fingers every time he uses a knife, but if he is careful he will probably only have that accident once every 1000 times.
One of the definitions of dangerous is "likely to cause problems or to have adverse consequences" which is exactly what happens if you write into an array out of bounds and it doesn't outright crash.
Well yes, I wont argue to that.
1

You don't need to pass the addresses of the variables at all, if you redefine assign_zero like this

void assign_zero(int *a, int n)
{
    for (int i = 0; i < n; i++)
        a[i] = 0;
}

it would work fine if you call it like

printf("Choose value of n: ");
if (scanf("%i", &n) == 1) /* never ignore the return value of any function, NEVER */
{
    int a[n];
    assign_zero(a, n);

    for (int x = 0 ; x < n ; x++)
        printf("%i", a[x]);
}

but the assign_zero function is completely unnecessary, you can just

#include <string.h>

and then

memset(a, 0, sizeof(a));

2 Comments

int a[n] = {0}; What compiler allows this? gcc complains that "variable-sized object may not be initialized"
@dbush Thanks, I was not sure and didn't want to lookup the standard document, but I suspected that it was not possible. I never knew about VLA's until I started participating on Stack Overflow, when I needed one, I used malloc() and haven't yet started to work with them because the need hasn't appeared in the work I am doing now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.