Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

d0ts420

New Coder
For some reason strcpy function doesn't work :/
I'm writing on Virtual Studio Code with gcc as compiler.

C:
#include <stdio.h>
#include <string.h>

int main(){

    char cars[1][10] = {"Fiat", "Lancia", "Alfa Romeo"};

    strcpy(cars[0] = "Tesla");

    for (int i = 0; i < sizeof(cars)/sizeof(cars[0]); i++)
    {
        printf("%s\n", cars[i]);
    }
    

    return 0;
}

Error:
[array of strings.c 2023-01-30 16:05:55.507]
,,array of strings.c: In function 'main':
array of strings.c:6:33: warning: excess elements in array initializer
6 | char cars[1][10] = {"Fiat", "Lancia", "Alfa Romeo"};
| ^~~~~~~~
array of strings.c:6:33: note: (near initialization for 'cars')
array of strings.c:6:43: warning: excess elements in array initializer
6 | char cars[1][10] = {"Fiat", "Lancia", "Alfa Romeo"};
| ^~~~~~~~~~~~
array of strings.c:6:43: note: (near initialization for 'cars')
array of strings.c:8:20: error: assignment to expression with array type
8 | strcpy(cars[0] = "Tesla");
| ^
array of strings.c:8:5: error: too few arguments to function 'strcpy'
8 | strcpy(cars[0] = "Tesla");
| ^~~~~~
In file included from array of strings.c:2:
C:/msys64/mingw64/include/string.h:61:18: note: declared here
61 | char * __cdecl strcpy(char * __restrict__ _Dest,const char * __restrict__ _Source);
| ^~~~~~
array of strings.c:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'long long unsigned int' [-Wsign-compare]
10 | for (int i = 0; i < sizeof(cars)/sizeof(cars[0]); i++)
|

I'm writing on Virtual Studio Code with gcc as compiler.
 
The code contains several issues:

  1. The cars array has only 1 element with 10 characters, but you are trying to store 3 strings in it.
  2. 'strcpy' copies a string from the source to the destination, but you are assigning the string "Tesla" to the destination, which will not work.
  3. The 'sizeof(cars)/sizeof(cars[0])' expression returns 1, but you expect it to return 3.
To fix these issues, you need to either change the definition of the 'cars' array to have 3 elements, or use an array of pointers to store the strings. Here's an example using an array of pointers:
Code:
#include <stdio.h>
#include <string.h>

int main()
{
    char *cars[] = {"Fiat", "Lancia", "Alfa Romeo"};
    strcpy(cars[0], "Tesla");

    for (int i = 0; i < sizeof(cars)/sizeof(cars[0]); i++)
    {
        printf("%s\n", cars[i]);
    }

    return 0;
}
 
Yes, you can put this code into a loop to work for all the movies. You can create an array of buttons or images representing each movie, and then attach the same event handler to all of them. In the event handler, you can determine which movie was clicked by comparing the movieTitle with the title of each movie in the list. Here's an example using an array of buttons:

Code:
private Button[] movieButtons;

@FXML
public void initialize() {
    movieButtons = new Button[]{Movie1, Movie2, Movie3, ...};
    for (Button movieButton : movieButtons) {
        movieButton.setOnMouseClicked(this::movieButtonClicked);
    }
}

private void movieButtonClicked(MouseEvent event) throws IOException {
    Button movieButton = (Button) event.getSource();
    String[] movieTitleArray = movieButton.getImage().getUrl().split("/");
    String movieTitle = movieTitleArray[movieTitleArray.length - 1];
    for (Movie temp : MainService.allMovies) {
        if ((temp.getMovieTitle() + ".jpg").equals(movieTitle)) {
            movie = temp;
            root = FXMLLoader.load(getClass().getResource("/SelectedMovie.fxml"));
            stage = (Stage) movieButton.getScene().getWindow();
            scene = new Scene(root);
            stage.setScene(scene);
            stage.show();
            break;
        }
    }
}
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom