This question already has answers here:
How can I correctly assign a new string value?
(4 answers)
Closed 5 years ago.
I want to make a matrix of strings in C Programming language
this is my code
void main()
{
char Data[10][3][20];
int i=0;
int j=0;
for (i=0;i<10;i++)
{
for (j=0;j<3;j++)
{
Data[i][j]="aa";
}
}
for (i=0;i<10;i++)
{
for (j=0;j<3;j++)
{
printf("%s",Data[i][j]);
}
}
printf("Done");
scanf("%d",&i);
}
the error am having is : assignment to expression with array type
please explain to me what am doing wrong because this is a prototype am trying to use in my original code that is to make a data base of "username,Password,level"
thank you inadvance.
Data[i][j] is an array. You can't assign to an array, only copy to it. use strcpy(). more details at http://www.cplusplus.com/reference/cstring/strcpy/
#include <stdio.h>
int main() {
char Data[10][3][20];
int i=0;
int j=0;
for (i=0;i<10;i++){
for (j=0;j<3;j++){
strcpy(Data[i][j], "aa"); //use strcpy for copy values
}
}
for (i=0;i<10;i++){
for (j=0;j<3;j++) {
printf("%s ",Data[i][j]);
}
printf("\n");
}
printf("Done");
scanf("%d",&i); //why this scanf here ??
return 0;
}
You are creating an array of char and you cannot assign (a pointer) to it. This is why you are getting the error assignment to expression with array type.
You can copy the string to the array elements though. Try using strcpy instead of the below assignment in your code:
Data[i][j]="aa";
Related
#include<stdio.h>
int main()
{
char str[5][10];
int i,j,k;
printf("Enter 5 strings:");
for(i=0;i<=4;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<=4;i++)
{
for(j=0;str[i][j]!='\0';j++);
for(k=j-1;k>=0;k--)
{
printf("%s",str[i][k]);
}
}
return 0;
}
please also explain my mistake and how to make such program without strrev() function i know it might be a dumb question but i am asking this because i am new to programming please help.
Is this what you were trying to do?
#include<stdio.h>
int main()
{
char str[5][10];
int i,j,k;
printf("Enter 5 strings:");
for(i=0;i<5;++i)
{
scanf("%s",str[i]);
}
for(i=0; i<5; ++i)
{
for(j=strlen(str[i])-1; j>=0; --j)
{
printf("%c", str[i][j]);
}
printf(" ");
}
return 0;
}
Enter 5 strings: Hello world this might work
Output:
olleH dlrow siht thgim krow
%s is for passing char* and printing a string. You should use %c to pass int and print one character via printf().
In other words, you should use
printf("%c",str[i][k]);
or
putchar(str[i][k]);
instead of
printf("%s",str[i][k]);
I am trying to make different 1d arrays from 1d array each of which separated with space .I am using this in a project which is causing a serious problem to my code if i use the second one . Even debugging didn't help me.
This generally aims at converting a statement with spaces into a different strings. The first code does the job for me but the second one is identical but causing segmentation fault .
For Example "This is C program" is input
The Output should be
"This" -must be in a[0],
"is" -must be in a[1],
"C" -must be in a[2],
"program" -must be in a[3].
Here is the first one
#include<stdio.h>
#include<stdlib.h>
void gets(char *);
main()
{
char str[100];
char **a;
int i=0,j=0,k=0,count=0,n;
printf("Enter a String : ");
gets(str);
while(str[k])
if(str[k++]==32)
count++;
count=count+1;
printf("%d\n",count);
a=calloc(count,sizeof(char *));
n=count;
for(i=0;i<count;i++)
{
k=0;
a[i]=calloc(1,20);
while((str[j]!=32)&&(str[j]!=0))
{
a[i][k]=str[j];
j++;
k++;
}
j++;
}
for(j=0;j<n;j++)
printf("%s\n",a[j]);
}
Here is the second one
#include<stdio.h>
#include<stdlib.h>
void gets(char *);
main()
{
char str[100];
char **a;
int i=0,j=0,k=0,count=0,n;
printf("Enter a String : ");
gets(str);
while(str[k])
if(str[k++]==32)
count++;
count=count+1;
printf("%d\n",count);
a=calloc(count,sizeof(char *));
n=count;
for(i=0;i<count;i++)
a[i]=calloc(1,20);
while(count)
{
while((str[j]!=32)&&(str[j]!=0))
{
a[i][k]=str[j];
j++;
k++;
}
j++;
i++;
k=0;
count--;
}
for(j=0;j<n;j++)
printf("%s\n",a[j]);
}
2nd loop lacks re-assignment of i. Deduced by OP after hint
for(i=0;i<count;i++)
a[i]=calloc(1,20);
// add
i = 0;
while(count)
{
while((str[j]!=32)&&(str[j]!=0))
{
// i has a value of count without re-assignment.
// This is UB as a[i] is eventually access outside range of a[]
a[i][k]=str[j];
j++;
k++;
}
...
Perhaps other issues?
the following code crashes if i give array of pointer here is there any other way to accept value through array of pointers or did i do somethong wrong here
the run this program after compiling you should type
objectname -numberoflines
//program to print first n lines of string using command line arguement
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int less(int x,int y);`enter code here`
int main(int argc,char* argv[])
{
int i,j,n,num;
char *lines[100];/*if I use two dimensional array here the code compiles
char nu[6];
// the whole for loop is for checking error in n
for(i=1,n=strlen(argv[1]);i<n;i++)
{
if(argv[1][i]=='.')
{
printf("your input was not correct \n");
return 0;
}
if(argv[1][i]=='-')
{
printf("your input was not correct \n");
return 0;
}
if(isalpha(argv[1][i])!=0)
{
printf("your input was not correct indeed");
return 0;
}
}
printf("\t\t application to print number of last n lines \n\n\n");
printf("enter the number of lines\n");
scanf("%d",&n);
printf("enter your lines \n");
for(j=0;(n<100)&&(j<=n);j++)
{
gets(lines[j]);
}
strcpy(nu,argv[1]);
nu[0]=' ';
num=atoi(nu);
num=(less(num,n));
for(i=0;i<=num;i++)
{
printf("%s",lines[i]);
}
return 0;
}
int less(int x,int y)
{
int z;
return (z=(x>y?y,printf("your input lines are less\n"):x));
}
The main problem is that when you write
char *lines[100];
You create an array of 100 char* pointers. These pointers have no memory allocated for them and they point to a random location. Writing to that location(using gets in your program) invokes Undefined Behavior.
To fix it, allocate memory for each pointer using
for(i=0 ; i<100 ; i++)
lines[i]=malloc(AMOUNT_OF_BYTES_TO_ALLOCATE);
And later, after the use is over, free the allocated memory using
for(i=0 ; i<100 ; i++)
free(lines[i]);
The reason that it worked when you used a two dimensional array is that you create an array of array of char for which memory is automatically allocated in the stack.
Help me to get out of this problem. I'm using GCC on ubuntu12.04. While I write this program to get 5 strings from keyboard n then print these strings on screen. Program is compiled but during execution it takes strings from keyboard but print only last string. The program which I have written is below:
void main()
{
char names[10];
int i,j;
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%s",names);
}
for(i=0;i<5;i++)
printf(" the names you enter are %s\n", names);
}
1) you can use 2D char array in this way
char names[5][100];
each line in the 2D array is an array of char with size = 100
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%99s",names[i]);
}
2) You can use array of pointers in this way
char *names[5];
each element in the array is a pointer to a string (char array). you have to assign each pointer in the array to a memory space before you call scanf()
for(i=0;i<5;i++)
{
names[i]=malloc(100);
printf(" Enter a name which you want to register\n");
scanf("%99s",names[i]);
}
3) if you compile with gcc version >2.7 then your scanf() can allocate memory by using "%ms" instead of "%s"
char *names[5];
for(i=0;i<5;i++)
{
printf(" Enter a name which you want to register\n");
scanf("%ms",&names[i]);
}
There is a simple example about reading and keeping string in the char array.
#include <stdio.h>
const int MACRO = 6;
int main() {
printf("Hello Admin Please Enter the Items:\n");
char items[MACRO][20];
for (int i = 0; i < MACRO; ++i) {
scanf("%19s", items[i]);
}
for (int i = 0; i < MACRO; ++i) {
printf("%s ", items[i]);
}
return 0;
}
In your program the mistake is that you have not putted '&'address of operator int the first for loop . names in your case is an array if you store %s string in names and not &names[0] or &names[1] or so on then as array itself acts as a pointer therefore the array "names" is pointing to the address of its first elements i.e. names[0] . so if you are writing scanf("%s",names); that is similar to scanf("%s",&names[0]); so as you are storing the names in one element only and that too for 5 iterations for only the last string you have entered will be stored and previous strings will be gone . so onlye last string is printed in your program .
in your code, you only declare char data type to be one dimensional and thus it will always overwrite the previous input,that's why the result is the last input printed 5 times.
char names[10];
the above declaration means that you declare a char type variable only with 10 character size without an extra array,it means you only declare a single variable for 5 input.
to make a two dimensional char, you will need to declare it like this :
char names[5][10];
in the code above, it means that you declare a char type variable with 10 character size in an array of 5.
Here is the code I wrote using pointer.
#include <stdio.h>
void main()
{
char *string[100];
int ln;
printf("Enter numbar of lines: ");
scanf("%d",&ln);
printf("\n");
for(int x=0;x<ln;x++)
{
printf("Enter line no - %d ",(x+1));
scanf("%ms",&string[x]); // I am using gcc to compile file, that's why using %ms to allocate memory.
}
printf("\n\n");
for(int x=0;x<ln;x++)
{
printf("Line No %d - %s \n",(x+1),string[x]);
}
}
Another code using two dimensional Array
#include <stdio.h>
void main()
{
int ln;
printf("Enter numbar of lines: ");
scanf("%d",&ln);
printf("\n");
char string[ln][10];
for(int x=0;x<ln;x++){
printf("Enter line no - %d ",(x+1));
scanf("%s",&string[x][0]);
}
for(int x=0;x<ln;x++)
{
printf("Line No %d - %s \n",(x+1),string[x]);
}
}
Though we declare a function with an integer array, we pass address of the array to the function. In the case of simple integers it gives error if we pass address we get pointer conversion error. But how its possible in case of an array
#include<stdio.h>
void print_array(int array[][100],int x, int y);
main()
{
int i,j,arr[100][100];
printf("Enter the array");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&arr[i][j]);
}
}
print_array(arr,i,j);
}
void print_array(int array[][100],int x,int y)
{
int i,j;
printf("\nThe values are\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++)
{
printf("%d",array[i][j]);
}
}
}
My question is even though our function is declared as one with integer array as first parameter (here) we are passing array address when we call the function. How does it function?
Your are passing the array, not its address.
arr is an int[][] array
(in fact it is pretty the same as &(arr[0]), which is a pointer to (the address of) the first line of your array. In C, there is no practical difference between an array and the corresponding pointer, except you take it's address with the & operator.)
Edit: Ok, just to make me clear:
#include <stdio.h>
int fn(char p1 [][100], char (*p2)[100])
{
if (sizeof(p1)!=sizeof(p2))
printf("I'm failed. %i <> %i\n",sizeof(p1),sizeof(p2));
else
printf("Feeling lucky. %i == %i\n",sizeof(p1),sizeof(p2));
}
int main()
{
char arr[5][100];
char (*p)[100]=&(arr[0]);
fn(arr, arr);
fn(p, p);
return 0;
}