-1

Can someone explain me, please, why can't I insert values into a struct array.

Here's a piece of my code to help understand what I'm trying to accomplish.

I want to insert values inside an array which is formed by structs, whenever I try to insert values it gives me a segmentation error.

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

#define TAM_TABELA 10
#define PRIMO 7
#define MAX_NOME 50

typedef struct {
    char nome[MAX_NOME];
    int telemovel;
} pessoa;

void insert(pessoa *grupo[]) {
    char nome[MAX_NOME];
    printf("\nInsert name:\n");
    scanf("%s", nome);
    int lenght = strnlen(nome, MAX_NOME);
    printf("\nInsert phone:\n");
    scanf("%d", &tel);

    for (i = 0; i < TAM_TABELA; i++) {
        if (grupo[i] == NULL) {
            grupo[i]->telemovel = tel;
            strcpy(grupo[i]->nome, nome);
            break;
        }
    }

    if (i == TAM_TABELA)
        printf("\nO valor não pode ser inserido!\n");
}

void main() {
    int opt;
    pessoa p;
    pessoa grupo[TAM_TABELA] = {NULL};

    insert(grupo);
}
Chris
  • 26,361
  • 5
  • 21
  • 42
Dario Santos
  • 74
  • 1
  • 8
  • Surely you get compiler warnings. The `insert` functions expects an argument of type `pessoa **`, but you are passing a `pessoa *`. Those are incompatible. – William Pursell Apr 20 '22 at 15:01
  • What are you trying to do with `if (grupo[i] == NULL) { grupo[i]->telemovel = tel;` ? That is an error. If `gropo[i]` is NULL, then attempting to reference the `telemovel` member of the struct with `grupo[i]->telemovel` is undefined behavior. If the pointer does not address a valid struct, what are you trying to access? – William Pursell Apr 20 '22 at 15:05
  • Thanks for the responses. I'm quite new to programming. I'm trying to insert values in the struct only if the struct is empty. I'm doing this because I'm tryng to implement a double hashing program. – Dario Santos Apr 20 '22 at 15:10
  • There is a difference between the struct being "empty" (which I suppose means uninitialized) and a pointer being NULL. If the pointer has a value of NULL, it does not point to a valid structure. That is, it points to memory that you can neither read nor write. This is very different than pointing to a structure which does not yet contain any valid data. – William Pursell Apr 20 '22 at 15:17

2 Answers2

0
  1. You need to pass the address of grupo to insert() function.
  2. int main(void) { } is a valid C main() function prototype
  3. size_t is a valid data type for lengths, iterate through arrays
  4. Always check whether scanf() conversion was successful or not
  5. Do not use "%s", use "%<WIDTH>s", to avoid buffer-overflow
  6. for( i = 0; i < TAM_TABELA; i++) i was never defined
  7. parameter of the function insert() should be pessoa ***grupo, in de-reference it later in your function like *grupo
  8. Initialize your char [] using {}, which are stored on stack memory
Darth-CodeX
  • 2,166
  • 1
  • 6
  • 23
0
   for (i = 0; i < TAM_TABELA; i++) {
       if (grupo[i] == NULL) {
           grupo[i]->telemovel = tel;
           strcpy(grupo[i]->nome, nome);
           break;
       }
   }

Notice how you've checked that grupo[i] is NULL and then you try to access a struct member on that value that is NULL.

You possibly want to check that grupo[i] != NULL, which should make member access safe.

Chris
  • 26,361
  • 5
  • 21
  • 42