I am making a program where the user enters 15 names names and determines if it is male or female, the problem I have is that the program ends abruptly after the input, I tried to use char*
but the compiler returnserror: invalid conversion from 'const char*' to 'char' [-fpermissive]
#include <stdio.h>
#include <string.h>
int main( ) {
char n[15],h[15],d[15], g[15];
int i,c=0,p=0,e;
printf("\nCuantos nombre desea ingresar?\n");
scanf("%d",&e);
for(i=0;i<e;i++){
printf("ingrese el nombre %d\n",i+1);
scanf("%s",&n[i]);
printf("escoja el genero de %s\n",n[i]);
scanf("%s",&g[i]);
if(g[i]=='m'){
n[i]=h[i];
c=c+1;
}
else if(g[i]=='f'){
n[i]=d[i];
p=p+1;
}
}
printf("-----------------\n");
printf("%d hombres: \n",c);
for(i=0;i<c;i++){
printf("%s\n",h[i]);
}
printf("%d mujeres: \n",p);
for(i=0;i<p;i++){
printf("%s\n",d[i]);
}
}
This is a 15-element array of type char:
And here you write in said vector:
Problems of these three lines? Two, basically:
You overwrite user data
As I have commented,
n
it is a vector of 15 elements of type char:So, on the first iteration you store a string starting at the position given by
n[0]
:On the second iteration, you store another string starting at the position of
n[1]
:And, as you can see, that causes the first chain to be lost.
buffer overflow
Continuing with what was said in the previous point, when
e
you have high values, the algorithm will start writing in the last positions of the array, then there will be less useful space to store the strings, which increases the risk that the code writes outside the array limits. For example, if we are in the 12th iteration, we can only enter a string of 2 characters:If we introduce a larger string we end up writing outside the limits of the array:
When this happens two things can happen:
To solve these problems you have to use a double array or matrix
You don't really need to use arrays in
n
. If you keep reading you will see why.Although of course, it may
n
not be the most appropriate name for a variable... they will not charge you any extra, so I suggest that the names of the variables be representative:Your code will be more readable and it will be more difficult for you to screw up when using variables.
Once you have already requested the name and gender, copy the data to another array (depending on the gender):
This code also has two problems:
The assignments are done just the other way around, that is, right now you are copying characters from
h
inton
and it should be the other way around. Also, note that you are copying characters, not strings.An important detail in C is that strings are copied using functions, like
strcpy
. If this function gives you errors it is probably because you are not trying to copy strings but, as in this case, single characters (the compiler can be your friend)As I mentioned before, you don't need to use arrays in
n
. The reason is that your code just asks for a name and copies it to another list based on gender.That is, the final lists must be arrays, but it
n
can be a string of characters to use:In this way, you reuse
nombres
to request the data from the user:For the array
h
you should use the indexc
instead ofi
Effectively, you use
c
andp
in what looks like it should be the indices for the two genre lists... but you don't take advantage of their values when populating those lists.Applying the fixes:
The problem you are seeing corresponds to this line:
This is because you are indicating that you are going to print a string (%s) however you give the function a char (n[i]).
C interprets a string as an array of chars terminated by the end-of-string character corresponding to
'\0'
. If your compiler didn't stop you, the program would start reading consequent memory locations an[i] looking for'\0'
until it would eventually hit a memory space that isn't reserved for it and you would get a segmentation fault or similar error as a result.You can check this by replacing the line with:
And you will see how the error is corrected because the printf function knows that it should only print one character.
Now this is where the problem comes in because you're actually wanting to save an entire string and not just one character. However, in your definition:
You are creating 4 arrays of 15 characters, in other words 4 strings. In array n you have 15 spaces for just 1 character.
A solution to this would be to create a 2-dimensional array, that is, an array of X number of strings of Y characters. And it would be declared like this:
This would create a list of size
MAX_CANT_NOMBRES
names of sizeMAX_TAMANO_NOMBRE
. The +1 is to make room for the end of string character'\0'
.Here I leave you an example so you can see how it would work, ignore the function
imprimirLista
, I made it just so you could see the result of the list that is created and add more advanced and confusing concepts.For a simpler reading we can parameterize with
#define
the sizes of the 2D array and usetypedef
something like this for the structures:I hope this doesn't raise more questions than answers, however it will pull you in the right direction, C is a language with a very steep learning curve.
Good luck in your programming!
The problem is that your array
n
does not have enough capacity for what it collectsscanf()
, the strings themselves are character strings (arrays):When you do
scanf("%s",&n[i]);
this you are telling the compiler to store in the first element of the arrayn
(the first iteration) the input string. You are entering a string into a space where only one character can enter.The solution to this is to create an array of arrays, where the arrays are the strings:
For this reason, it is also advisable to define the arrays in the same way
h
andd
that they must have the same capacity asn
, this is because they will eventually store an element ofn
.Another thing I noticed, is that you do assignments like this:
This is not valid in the first instance, since the assignment must be done from left to right, not the other way around, because at that moment that index
h
is empty.It can also generate errors, it is dangerous. It is always better to make use of
strcpy()
, defined in the header<string.h>
:In short, the arrays must be defined like this:
and the first loop
for
should look like this:I hope I have served you, greetings.