1

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"

7
  • 2
    Compile with -Wall. Also you never initialise name. Commented May 15, 2020 at 0:30
  • 2
    Because *concts[MAX] is an array of pointers those pointing to no allocated memory, so you should allocate memory for each pointer or just declare it for example like char concts[MAX][20] Commented May 15, 2020 at 0:35
  • 1
    name is a pointer and can't store a string, use an array of chars instead. Commented May 15, 2020 at 0:37
  • 1
    "it compiles well and does not prompt errors." Compile with -Wall -Werror to fix that false perception. Commented May 15, 2020 at 0:40
  • 1
    No that allocates memory for one string. For multiple strings, you either need to malloc memory for each string, or declare a 2D array. For example char concts[10][32] gives you room for 10 strings of 32 bytes each. And char *name should be char name[32]. Commented May 15, 2020 at 1:03

3 Answers 3

2

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;
}
Sign up to request clarification or add additional context in comments.

5 Comments

Make sure you only enter names up to 19 characters.
Yeah I don't know how much chars he expects but it's up to him to decide, for me it's just an example, he can change the size the way he need
Note: You don't need the & when giving a string argument to scanf; scanf("%s", name); will do.
Ah I'm sorry I didn't even see it, because I was focusing on the main problem, thanks I have just edited the answer
Thanks, this answere works well for me, ran fine and does what i expected.
1

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);
    }
}

Comments

1

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 *

Comments

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.