I am learning 2d arrays in C and im trying to get the user's name in C and store it in a 2d array. I found out that C don't have String data type and can only accepts string with data type char.
Now I am successful in getting the name from the user but i can't store the name into the separate 2d array. Here the code i tried:
#include <stdio.h>
#include <cstring>
int main()
{
int to=0,y=1,z=0,end=0;
char *ticketS[100][3];
char name[50];
printf("\nENTER PASSENGER'S NAME: ");
gets(name);
printf("%s",name);
strcpy(ticketS[z][0], name);
printf("%s", ticketS[z][0]);
return 0;
}
My input is: test name
My expected output is:
test name
test name
The actual output is:
test name
I can't seem to find any examples regarding my problem. Any help would be greatly appreciated.
Assuming you wanted to create 2 arrays with 100 chars each, and copy the name given by the user to the 2D array:
#include <stdio.h>
#include <string.h>
int main()
{
int to=0,y=1,z=0,end=0;
char ticketS[2][100];
char name[50];
printf("\nENTER PASSENGER'S NAME: ");
gets(name);
printf("%s\n",name);
strcpy(ticketS[0], name); // a for-loop would work as well
strcpy(ticketS[1], name);
printf("%s\n%s\n", ticketS[0], ticketS[1]);
return 0;
}
A few things you should pay attention to:
The indexing is wrong. char *ticketS[100][3]; creates 100 arrays with 3 chars each.
Using char *ticketS[100][3]; you just declare the variable, however, you don't initialize it
Null-terminator '\0' here plays a role. strcpy copies until it finds a Null-terminator. Therefore, to get your wanted result you would need to copy the string a few times, or use a different copying method (not strcpy).
This is also why I used %s twice with printf.
In the code exmaple I gave, ticketS[0] is a pointer to the 1st 100 char array.
Hope you find this useful.
Related
I'm new to C and i'm trying to store some strings inside a 2D array of chars.Here's what i have:
char strArray[100][100];
char input[100];
scanf("%s",&input);
strArray[i] = input; //this is where i get the incompatible types assignment error
As shown in the comment i get an incompatible types in assignment error.Do i need to use an array of char *strArray[100][100] ? Aren't strArray and input both of the same type (char [])? The one's 1D and the other is 2D obviously but i just didn't specify the 2nd dimension in the assignment since each string is stored in a new line. What am i doing wrong?
You'd have to use strcpy():
#include <stdio.h>
#include <string.h>
int main(void)
{
char strArray[100][100];
char input[100];
scanf("%s", input);
strcpy(strArray[0], input);
}
But never, really: never! use scanf() with "%s" without limiting the number of characters to read (field width):
scanf("%99s", input);
You can use strcpy() to copy each char in input to strArray[i]. In this case you would use
strcpy(strArray[i], input);
In C, you cannot assign arrays in the sense of char input1[100], input2[100]; input1 = input2. You can just copy the contents using, for example, strcpy for strings or memcpy for arbitrary memory chunks.
So you'd have to write strcpy(strArray[i],input), provided that i is an integral value between 0 and 99 in your case.
Further, you'll have to omit the & in scanf("%s",&input) (i.e. write scanf("%s",input)), because input already decays to a pointer to char.
I relatively new to the C language and StackOverflow. I'm trying to write a simple C code that will prompt user for their name and then display it.
#include <stdio.h>
int main (void)
{
char name;
printf("Let's do this, please enter your name:");
scanf("%s", &name);
printf("Your name is %s", name);
return 0;
}
The code complies but after inputting the name, it displays Segmentation fault (core dumped).
Thank you for any help.
A very small mistake #Jackie_Legs. In the declaration of the variable name, You have declared it as a char. so it holds just one character.
The solution: choose an arbitrary size for your name, say 10 or 15 characters. and declare it as an array of the size.
char name[15];
No changes in any other part of the program. Also, you should omit the & symbol in the scanf for strings.
So just one change and your code should work.
Here is the updated code which would work:
#include <stdio.h>
int main (void)
{
char name[15];
printf("Let's do this, please enter your name:");
scanf("%s", name);
printf("Your name is %s\n", name);
return 0;
}
It is because your name variable can only store single character and you are trying to store more than one character which will lead to unpredictable behaviour of program (e.g. segmentation fault).
If you know max length of name, declare variable name as array of character such as
char name[20];
Here you can store name with max length of 19 character. You can decide length of array as per your requirement.
You need to declare a string as because a single letter is stored in char and multiple characters along with a null character at the end forms an String which can be anything like names, etc.
In your code you have taken char which stores only a single character but in order to store your name (which is a string) you will have to take char array along with the size of the array.
Replace char with char[size] where size is the size for the string that you need.
Here are the changes I made to your code:
#include <stdio.h>
int main (void)
{
char name[30];
printf("Let's do this, please enter your name:");
scanf("%s", name);
printf("Your name is %s", name);
return 0;
}
name is declared as char, which means it can contain only one character. It is not sufficient to contain a string of characters (which a name usually consists of). You need to declare a char array (an array of characters).
The size of the array should be at least 1 more than the largest name you want to read in.
The extra byte is to contain the null terminating character '\0'.
char name[SizeOfLargestName + 1];
And when you use scanf, you need not use & because now name points to the first byte of the array.
scanf("%s", name);
It is throwing error because of char name; char data type in C contain 1 character only in a variable. But your input is a string.
You need to change the declaration of the variable from char to char array.
char name[50];
printf("Let's do this, please enter your name:");
scanf("%s", name);
printf("Your name is %s", name);
I expected to get errors in following code, but I did not. I did not use & sign. Also I am editing array of chars.
#include <stdio.h>
int main()
{
char name[10] ="yasser";
printf("%s\n",name);
// there is no error ,
// trying to edit array of chars,
// also did not use & sign.
scanf("%s",name);
// did not use strcpy function also.
printf("%s\n",name);
return 0;
}
I expected to get errors in following code, but I did not.I did not use & sign.
scanf("%s",name);
That's totally ok as name is already the address of the character array.
It sounds like you have several questions:
calling scanf("%s", name) should have given an error, since %s expects a pointer and name is an array? But as others have explained, when you use an array in an expression like this, what you always get (automatically) is a pointer to the array's first element, just as if you had written scanf("%s", &name[0]).
Having scanf write into name should have given an error, since name was initialized with a string constant? Well, that's how it was initialized, but name really is an array, so you're free to write to it (as long as you don't write more than 10 characters into it, of course). See more on this below.
Characters got copied around, even though you didn't call strcpy? No real surprise, there. Again, scanf just wrote into your array.
Let's take a slightly closer look at what you did write, and what you didn't write.
When you declare and initialize an array of char, it's completely different than when you declare and initialize a pointer to char. When you wrote
char name[10] = "yasser";
what the compiler did for you was sort of as if you had written
char name[10];
strcpy(name, "yasser");
That is, the compiler arranges to initialize the contents of the array with the characters from the string constant, but what you get is an ordinary, writable array (not an unwritable, constant string constant).
If, on the other hand, you had written
char *namep = "yasser";
scanf("%s", namep);
you would have gotten the problems you expected. In this case, namep is a pointer, not an array. It's initialized to point to the string constant "yasser", which is not writable. When scanf tried to write to this memory, you probably would have gotten an error.
When you pass arrays to functions in C, they decay to pointers to the first item.
Therefore for:
char name[] ="yasser";
scanf("%s", name) is the same as scanf("%s", &name[0]) and either of those invocations should send shivers down your spine, because unless you control what's on your stdin (which you usually don't), you're reading a potentially very long string into a limited buffer, which is a segmentation fault waiting to happen (or worse, undefined behavior).
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv, char **envp) {
char *myName = (char *) calloc(10, sizeof(char));
*(myName)='K'; *(myName+1)='h'; *(myName+2)='a'; *(myName+3)='l'; *(myName+4)='i'; *(myName+5)='d';
printf("%s\n",myName);
scanf("%s",myName);
printf("%s\n",myName);
return (EXIT_SUCCESS);
}
#include <stdio.h>
#include <string.h>
int main()//fonction principale
{
char name[10] ="yasser";
int longeur=0;
printf("%s\n",name);
scanf("%s",name);
longeur = strlen(name);
for (int i=0;i<longeur;i++) {
printf("%c",*(name+i));
}
return 0;}
#include <stdio.h>
#include <string.h>
int main(void) {
int number_of_members;
char family[number_of_members][20][number_of_members][20];
char member_name[20];
char birth_state[20];
char family_last_name[20];
printf("What is the last name of the family?\n");
scanf("%s", &family_last_name);
printf("How many members do you want to create?\n");
scanf("%d", &number_of_members);
int const FAMILY_SIZE = number_of_members;
number_of_members = number_of_members -1;
printf("Enter the family member name: \n");
for(number_of_members;number_of_members>-1;number_of_members--)
{
scanf("%s", &member_name);
strcpy(family[number_of_members], member_name);
printf(" %d %s %s\n",number_of_members, member_name, family_last_name);
}
printf("%s, %s ", family[0], family[1]);
return 0;
}
Here is the output:(from Ideone.com)
Ideone.com with code
The input to this code is: Layne , 2 , tim , jim.
When run, it shows the correct index with the name in the array however, once out it will show the last entered name, jim, as family1 and family[0]. Am I not understanding how strcpy() works? or is it a logic error?Some assistance soon would be appreciated!
This is very very wrong
int number_of_members;
char family[number_of_members][20][number_of_members][20];
Because you haven't initialized number_of_members.
Because it doesn't make sense whatsoever, it's not possible that you really need this kind of array.
And yes, if you enable compiler warnings it will hit you in your nose with a stick, because
strcpy(family[number_of_members], member_name);
shouldn't even compile and is undefined behavior since the type of family[number_of_members], is an array of arrays of arrays of char.
strcpy can take an array of char's because it will be automatically converted to a char poitner, and provided that the contents of the array comply with what a c string is, then strcpy() will work correctly, in your case the behavior is undefined because almost surely the '\0' will never be found in the destination pointer.
instead of
int num_of_members;
char family[number_of_members][20][number_of_members][20];
which is not C code, do this
#define MAX_MEMBERS 20
char family[MAX_MEMBERS][20];
which creates a rectangular array of arrays each of 20 bytes long
I keep receiving this error and unsure how to fix it. I'm new to C programming and tried searching through the book/internet and couldn't find much help. I'm trying to create a program that will print a grade report using a loop and a sctructure
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Grades
{
char Name[20];
char Hrs;
int ID;
char ClassName[20];
char Grade;
char ClassID[6];
};
int main ()
{
struct Grades Transcript[6];
int classCnt = 0;
int vHrs=0;
char vGrade[2];
char vName[20], vCID[6], vClassName[20];
printf("Enter Students Name: ");
fgets(vName, 20, stdin);
do
{ printf("Enter Class ID; ");
fgets(vCID, 6, stdin);
strcpy_s(Transcript[classCnt].ClassID, vCID);
printf("Enter Class Name: ");
fgets(vClassName, 20, stdin);
strcpy_s(Transcript[classCnt].ClassName, vClassName);
printf("Enter Class Hours: ");
scanf("%d", &vHrs);
strcpy(Transcript[classCnt].Hrs, vHrs); //Problem occurs here
printf("Enter Class Grade: ");
scanf("%c", vGrade);
strcpy(Transcript[classCnt].Grade, vGrade); //Problem occurs here
classCnt++;
}while(classCnt<=6);
}
You actually have a number of problems here:
First, strcpy() is used to copy a string, if you have a character and you want it to assign it, you can simply assign it with the = operator. The strcpy() function is used when you have a character array you want to assign.
So your first problem
strcpy(Transcript[classCnt].Hrs, vHrs);
Hrs from your struct is just a char type, and vHrs is an int type. You can simply assign it like:
Transcript[classCnt].Hrs = vHrs;
However, an int can hold a lot more data than a char can, this is going to give you a warning about overflow and you should listen to it (depending on the implementation char holds -128 to 127, where as int holds −2,147,483,648 to 2,147,483,647). Decide what data type you really wanted here and either make Hrs an int or vHrs a char then just do the assignment.
Second problem:
scanf("%c", vGrade);
vGrade as a character array (it is made up of more than one character) that means you should assign it with the string format operator "%s", but when you do a string you should make the array long enough for the number of characters you want + 1 (for the NULL terminator).
Third problem:
strcpy(Transcript[classCnt].Grade, vGrade);
Grade is a char whereas vGrade is an array. Again, you have to make a decision of type, if you wanted a "string" of characters then you need to make them both arrays, if you wanted just a single character then change the type of vGrade and do a simple assignment with the = operator.
The thing you're running into here is a data typing error.
The structure member .Hrs is of type "char". The argument to strcpy is a "char *". Think of it like this: A char is a single 8 byte number representing a value in ASCII. A "char *" is a pointer to an array of characters (or a string in C language).
The Above is traditionally true,but , today it might be a unicode or multi-byte character string, as well. In either case, what you need is the assignment operator (=), not a strcpy.
So you're telling strcpy to look at a single value (char), not a string of values (char *) which is it expecting as it's argument. In this case, you can just copy the value of the integer you scanned in with scanf directly.
Transcript[classCnt].Hrs = vHrs;
and
Transcript[classCnt].Grade = vGrade;
If you're looking for the best book ever written, well in my opinion anyway :-), on C Language, check out:
C Programming Language (2nd Edition) - Looks like it's available on Amazon (or at you're favorite used book seller) for about $21 (USD).