Thread: How can I pass, by reference, a string in a struct, to a function ?

  1. #1
    Registered User
    Join Date
    May 2016
    Location
    Northeast US
    Posts
    5

    How can I pass, by reference, a string in a struct, to a function ?

    I'm using a PIC32MZ and want to open a number of .bmp files and then move their image data into a SRAM. I know the names of the .bmp files and need to extract the height, width, etc and then use this info to properly transfer and store the image data.

    So, I set up a typedef struct to define a type that will contain the image info. I strcpy the file name into the struct in main(). I want to call a function to open the file, extract the image data, and put it into my SRAM.

    My simplified code is below. I'm unable to pass the image file name to the function. The error is "error: incompatible types when assigning to type 'char[64]' from type 'char (*)[64]'

    What am I doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    
    typedef struct{
        char                    imageFileName[64];
        uint16_t                imageHeight;
        uint16_t                imageWidth;
        uint32_t                imageStart;
    } IMAGE_DATA;
    
    IMAGE_DATA                  imageData;
    IMAGE_DATA                  running;
    
    char                        bmp_fileName[64];
    uint16_t                    bmp_height;
    uint16_t                    bmp_width;
    uint16_t                    bmp_start;
    
    void Load_Image(IMAGE_DATA *);
    
    main(){
        strcpy(running.imageFileName,"Running16.bmp");
        running.imageHeight = 128;
        running.imageWidth  = 128;
        running.imageStart  = 84;
    
        Load_Image(&running);
    }
    
    void Load_Image(IMAGE_DATA *imageData){
        bmp_fileName = &(imageData->imageFileName);  // error is here
        bmp_height = &(imageData->imageHeight);
        bmp_width = &(imageData->imageWidth);
        bmp_start = &(imageData->imageStart);
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't use the assignment operator= with C-strings, you need to use strcpy() like you did on line 24.

    Jim

  3. #3
    Registered User
    Join Date
    May 2016
    Location
    Northeast US
    Posts
    5
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pass by reference by inner function in C
    By shanwu in forum C Programming
    Replies: 6
    Last Post: 06-05-2012, 10:02 PM
  2. Knowing when function needs pass by reference.
    By bsdunx in forum C Programming
    Replies: 4
    Last Post: 08-29-2007, 12:38 PM
  3. Pass an array to a function by reference?
    By Loic in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2007, 11:44 PM
  4. Pass struct to a function.
    By Hybird in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2005, 12:12 AM
  5. Ask about function parameters and pass a reference.
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2002, 12:14 PM

Tags for this Thread