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];

5
  • What is your "segmentation error"? I got compilation error. Commented Aug 1, 2020 at 7: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 *). Commented Aug 1, 2020 at 7:55
  • Also you may want to see c - srand() — why call it only once? - Stack Overflow Commented Aug 1, 2020 at 7: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). See also: What should main() return in C and C++? Commented Aug 1, 2020 at 7: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(). Commented Aug 1, 2020 at 8:06

3 Answers 3

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

2 Comments

Thanx a lot for your explanation :) @RobertS supports Monica Cellio
@sowmicaML I'm glad that I could help.
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.

2 Comments

Note, you should use const char *movie; to make clear what movie points to is immutable. (compared to a mutable result provided by @MikeCAT)
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.
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);

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.