artic sol
artic sol

Reputation: 493

c program how to print a char array held by a structure variable?

How do I print the char array held in the struct variable char binary_filename?

I tried:

printf("Binary file name is : %s \n", prv_instance_t.binary_filename);

However, I get the error error: expected expression before ‘prv_instance_t’

Here is the struct definition.

#define BINARY_FILE_NAME_MAXLEN   10 

typedef struct _prv_instance_
{
    /*
     * The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
     * is the instance scope user data (uint8_t power in this case)
     */
    struct _prv_instance_ * next;   // matches lwm2m_list_t::next
    uint16_t shortID;               // matches lwm2m_list_t::id
    uint8_t  power;
    uint8_t  reset;
    double   dec;
    char binary_filename[BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;

Upvotes: 0

Views: 1694

Answers (2)

VolAnd
VolAnd

Reputation: 6407

Just remove typedef keyword.

Current definition does not define a variable, but makes two equivalent types prv_instance_t and struct _prv_instance_. From your question I understand that prv_instance_t should be an instance (variable) of type _prv_instance_, keyword typedef is unnecessary.

Your printf will work with:

#define BINARY_FILE_NAME_MAXLEN   10

struct _prv_instance_
{
    /*
    * The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
    * is the instance scope user data (uint8_t power in this case)
    */
    struct _prv_instance_ * next;   // matches lwm2m_list_t::next
    uint16_t shortID;               // matches lwm2m_list_t::id
    uint8_t  power;
    uint8_t  reset;
    double   dec;
    char binary_filename[BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;

as well as with the following one:

#define BINARY_FILE_NAME_MAXLEN   10

struct _prv_instance_
{
    /*
    * The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
    * is the instance scope user data (uint8_t power in this case)
    */
    struct _prv_instance_ * next;   // matches lwm2m_list_t::next
    uint16_t shortID;               // matches lwm2m_list_t::id
    uint8_t  power;
    uint8_t  reset;
    double   dec;
    char binary_filename[BINARY_FILE_NAME_MAXLEN];
};

struct _prv_instance_ prv_instance_t;

Upvotes: 0

Mostafa
Mostafa

Reputation: 468

You are using the type itself. To access a member of the struct you have to declare an instance of that struct first. For example, this will print Hello World :

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

#define BINARY_FILE_NAME_MAXLEN 10

typedef struct _prv_instance_
{
    char binary_filename [BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;


int main()
{
    prv_instance_t foo, bar;
    strcpy(foo.binary_filename, "Hello");
    strcpy(bar.binary_filename, "World");

    printf("%s %s\n", foo.binary_filename, bar.binary_filename);
    return 0;
}

What you are trying to do is similar to

printf("%d", int);

Upvotes: 1

Related Questions