1
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
 char movie[15];
 char movielist[25][30]={"thalaiva","aruvi","sarkar","visvasam","veeram","mersal","bigil","kaithi","mahanadhi","papanasam","vishwaroopam","padayappa","thadam","indian","petta","kaala","psycho","comali","bahubali","saaho","enthiran","vettai","asuraguru","penguin","cocktai"};
 movie=movielist[rand()%25];
 printf("%s",movie);

}

I want the variable movie to store any random string from the array movielist! The code above gives me error. main.c:8:7: error: assignment to expression with array type movie=movielist[rand()%25];

sowmica ML
  • 17
  • 1
  • 7
  • What is your "segmentation error"? I got compilation error. – MikeCAT Aug 01 '20 at 07:54
  • 1
    If you want to make a copy use `strcpy` not assignment as that is not how you copy a string. If you just want to reference the original string then use a char pointer (`char *`). – kaylum Aug 01 '20 at 07:55
  • Also you may want to see [c - srand() — why call it only once? - Stack Overflow](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) – MikeCAT Aug 01 '20 at 07:58
  • Unless you are programming in a *freestanding environment* (without the benefit of any OS), in a standards conforming implementation, the allowable declarations for `main` for are `int main (void)` and `int main (int argc, char *argv[])` (which you will see written with the equivalent `char **argv`). See: [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin Aug 01 '20 at 07:58
  • Avoid the use of the ancient DOS header `conio.h`, that makes your code 100% non-portable. (you don't use anything from that header in your code above) You can use `getchar()` to hold the terminal window open on windows, instead of `getch()`. – David C. Rankin Aug 01 '20 at 08:06

3 Answers3

3

Your code has two main issues.

1.

With

movie = movielist[rand() % 25];

you attempt to assign movie(an array of 15 char, type char [15]) by a pointer to an array of 30 char (type char (*)[30]). This is not permissible in C. You cannot assign arrays.

Since you don't want to modify/change the movie titles/strings in the program, you don't need a two-dimensional array for movielist and an array for movie.

Use an array of pointer for movielist which point to string literals and a pointer for movie instead.

The pointer move you assign then by the value of one of the pointers in movielist.


2.

rand() needs a seed with srand(). Without that, rand() won't work properly. But you forgot that in your code.

A usual way is to provide a seed dependent upon the CPU clock time.


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>       // for the time seed.

int main (void)
{
   char const * movie;
   char const * const movielist[] = { 
                                       "thalaiva","aruvi","sarkar","visvasam","veeram",
                                       "mersal","bigil","kaithi","mahanadhi","papanasam",
                                       "vishwaroopam","padayappa","thadam","indian",
                                       "petta","kaala","psycho","comali","bahubali",
                                       "saaho","enthiran","vettai","asuraguru","penguin",
                                       "cocktai"
                                    };

   // initialize random seed.
   srand(time(NULL));

   movie = movielist[rand() % 25];

   printf("%s", movie);
}

Side note:

  • void main() is not compliant to the C standard. Use int main (void).
1

Use char pointer instead of char array:

char *movie;  // instead of char movie[15];
...
movie = movielist[rand() % 25];

If you use movie[15] as you did, you can't assign it to individual item in the movielist as assignment operator does not work with array.

artm
  • 17,291
  • 6
  • 38
  • 54
  • 2
    Note, you should use `const char *movie;` to make clear what `movie` points to is immutable. (compared to a mutable result provided by @MikeCAT) – David C. Rankin Aug 01 '20 at 08:02
  • Also understand using a pointer does not store a string from your array, it simply holds the address to an existing element within the array. – David C. Rankin Aug 01 '20 at 08:08
1

You can use strcpy() for copying strings.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
 char movie[30];
 char movielist[25][30]={"thalaiva","aruvi","sarkar","visvasam","veeram","mersal","bigil","kaithi","mahanadhi","papanasam","vishwaroopam","padayappa","thadam","indian","petta","kaala","psycho","comali","bahubali","saaho","enthiran","vettai","asuraguru","penguin","cocktai"};
 strcpy(movie,movielist[rand()%25]);
 printf("%s",movie);

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70