Alejandro Gomez
Alejandro Gomez

Reputation: 80

C program stops after traing to access string array

i'm making a program in C that let the user insert the number of names that he wants, this names is stored in a global array and then they are printed, but the program finishes before, specifically when i try to access to the global array to print the names to show them to the user. I need to have my global array as follows: char *array[10]. This problem just happens when i use the previus syntax, but when i use: char array[10][], all runs fine, what's the problem here? somebody can help me please, i have tried so many hours.

CODE:

#include <stdio.h>

#define MAX 10
int counter = 0;
char *concts[MAX];

void add_2(char *name){
    printf("Se anadira un elemento en el index %d\n", counter);
    concts[counter] = name;
    counter++;
}

void main(){
    char *name;
    int ingresando = 1, i;

    do{
        printf("ingresa un nombre: ");
        scanf("%s", &name);
        add_2(name);
        printf("Seguir ingresando? ");
        scanf("%d", &ingresando);
    }while(ingresando == 1);

    printf("Terminado. contador: %d\n", counter);

    for(i = 0; i < counter; i++){
        char *otherName = concts[i];
        printf("%s\n", otherName);
    }
}

PROBLEM: I don't really know, but the program ends before what is expected, it compiles well and does not prompt errors.

EDIT: The program stops after print "Terminado. contador: %d\n"

Upvotes: 1

Views: 182

Answers (3)

mmnsh
mmnsh

Reputation: 19

The above answers seem right but the other way is using malloc.

first you must include <stdlib.h> and then edit line 12(char *name) like this

char *name=malloc(21);

*for old version of gcc you must cast the output of malloc to char *

Upvotes: 1

Saadi Toumi Fouad
Saadi Toumi Fouad

Reputation: 2829

Here I made some changes

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

#define MAX 10
int counter = 0;
char concts[MAX][20];

void add_2(char *name){
    printf("Se anadira un elemento en el index %d\n", counter);
    strcpy(concts[counter], name);
    counter++;
}

int main(){
    char name[20];
    int ingresando = 1, i;

    do{
        printf("ingresa un nombre: ");
        scanf("%s", name);
        add_2(name);
        printf("Seguir ingresando? ");
        scanf("%d", &ingresando);
    }while(ingresando == 1);

    printf("Terminado. contador: %d\n", counter);

    for(i = 0; i < counter; i++){
        char *name = concts[i];
        printf("%s\n", name);
    }
    return 0;
}

Upvotes: 2

Ali Hassan
Ali Hassan

Reputation: 966

Now run it.

#include <stdio.h>
#include <string.h>
void add_2(char *name); //defining prototype of function
#define MAX 10
int counter = 0;
char concts[MAX][20];
void add_2(char *name){
    printf("Se anadira un elemento en el index %d\n", counter);
    strcpy(concts[counter], name);
    counter++;
}
main(){
    char name[20];
    int ingresando = 1, i;
    do{printf("ingresa un nombre: ");
        scanf("%s", &name);
        add_2(name);
        printf("Seguir ingresando? ");
        scanf("%d", &ingresando);
    }while(ingresando == 1);
    printf("Terminado. contador: %d\n", counter);

    for(i = 0; i < counter; i++){
        char *name = concts[i];
        printf("%s\n", name);
    }
}

Upvotes: 1

Related Questions