How to initialize an array using pointers?

Asked

Viewed 12,254 times

-7

I need to initialize a 0 matrix using pointer to address its elements

  • 18

    What did you try? Enter your code that is not working.

  • -1. Fabao, this is a classic question of course, and you have shown no effort or described well what you wanted. Please try a little harder at least when asking a question like this

3 answers

5

See if this is what you want:

int main()
{
    int matriz[5][5], i, *ponteiro;
    ponteiro = matriz[0];
    for (i = 0; i < 25; i++)
    {
        *ponteiro = 0;
        ponteiro++;
    }

    return 0;
}

It’s pretty much the same thing that was answered in your question vector sum with pointers.

  • 8

    Guys, it’s exercise, let the OP work a little harder instead of handing it all in hand...

4

Four ways to zero a matrix come to mind, among them the easiest would be this:

int matriz[5][5] = {0};

The normal, basic level, that would be:

int matriz[5][5], i, j;
for(i=0; i<5; i++)
{
    for(j=0; j<5; j++)
    {
        matriz[i][j]=0;
    }
}

A middleman, I think that’s the one you want, would be to work with pointers. As the allocation is sequential, the next or previous values will always be in the next house, going from the beginning to the end of matriz. I only see use for something of the kind in an auxiliary function, because that way it is completely a waste of time, there is no need.

int matriz[5][5], i, j, *ptr=matriz;
for (i=0; i<25; i++, ptr++)
{
    *ptr=0;
}

And it has the advanced method, with memset(), here you mess with memory directly, the first argument is the vector, the second is the value to be setate and the third the amount of values of the vector to be setate in bytes. You first need to know what kind of data you are working with, its size in bytes, it is used function sizeof() that takes the data and returns the amount of bytes allocated to that data. So, to reset matriz you could do just that:

memset(matriz, 0, sizeof(matriz));
  • 1

    Just complementing: Although it sounds counterintuitive, int matriz[5][5] = {1}; nay will produce an array of ones. What happens is that if you do not specify all the elements in the boot list, they will be zero. However you must specify at least one element ({} is not valid). So {1} produces the first element 1 and or other 0.

  • 1

    Yes, you are correct, William. And you just reminded me of another resource for a case like this described by you: int matriz[5][5] = { [0 ... 4][0 ... 4] = 1 };. Are the so-called designated initiators

4

#include <string.h>

/* ... */

int matrix[3][4];
memset(matrix, 0, 3 * 4 * sizeof(int));

The function memset receives a pointer, a value and a quantity. It will fill that amount of bytes from the pointer with that value.

Since the size is in bytes, the quantity must be multiplied by the size of the matrix type.

Edit: As suggested in the comment of that reply, it is possible to do the following as well:

memset(matrix, 0, sizeof(matrix));

This is possible because the compiler knows the full size of the matrix, since its dimensions are defined in the variable. But if you instead had a pointer to the matrix, that wouldn’t be possible:

int *ptr = &matrix[0][0];
memset(ptr, 0, sizeof(ptr));         /* ERRADO:  vai zerar apenas o primeiro elemento */
memset(ptr, 0, 3 * 4 * sizeof(int)); /* CORRETO: vai zerar toda a matriz */
  • 1

    Or else memset(matrix, 0, sizeof(matrix));. (I think the point was an exercise and he wanted a loop, but memset is indeed the right way to do it. + 1)

  • @Guilhermebernal I certainly did not suggest this just to make the answer clear to him. And it is probably an exercise yes. But the statement did not specify that it had to loop, or that it could not use standard library functions :)

  • 7

    Guys, it’s exercise, let the OP work a little harder instead of handing it all in hand...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.