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

#define MAX_BUFFER_SIZE 100   

typedef struct config_struct config_t ;

struct config_struct{
    char name[20];
        char ip[20];
        int port;
        char log[100];
};

int main(int argc, char *argv[]) {
        config_t config;
        read_config_file(argv[1], &config);
        return 0;
}

int read_int_from_config_line(char* config_line) {
        int val = atoi(config_line);
        return val;
}

void read_str_from_config_line(char* config_line, char* val) {
        strncpy(val,config_line, MAX_BUFFER_SIZE);
}

void read_config_file(char* config_filename, config_t *config) {
        FILE *fp;
        char *value, buffer[MAX_BUFFER_SIZE];
        fp = fopen("./settings.conf", "r");
        while (fgets(buffer, MAX_BUFFER_SIZE, fp) != NULL) {
            if(!strcmp(buffer,"NAME")) {
                    read_str_from_config_line(value, &(config->name));
        }   
            if(!strcmp(buffer,"PORT")) {
                &(config->port) = read_int_from_config_line(value);
                }

        }
}

I try to compile this code and it gives me a incompatible pointer type at read_str_from_config_line(value , &(config->name)); and lvalue required as left operand of assignment &(config->port) = read_int_from_config_line(value);

I am trying to return the stuct back to the main program but I'm having problem with the struct pointers.

Any solution for it to be solve? PS: The code is a mess as I trying to shorten it. Any help will be good as I am still a beginner to C programming.

Thanks

6
  • If you use array as left operand of = without [] operator it is automatically 'casted' into pointer to first element of the array. Try it without & operator, because it returns R value pointer to pointer value (I guess). Commented Mar 29, 2017 at 10:05
  • @nikachx you can not assign to an array. Commented Mar 29, 2017 at 10:06
  • Both errors can fixed by removing &. Commented Mar 29, 2017 at 10:07
  • 1) if(!strcmp(buffer,"NAME")) { --> if(!strcmp(buffer,"NAME\n")) { Commented Mar 29, 2017 at 10:07
  • @KamiKaze Ok, I think you are right. Commented Mar 29, 2017 at 10:07

2 Answers 2

2

The & operator means "take the address of the object to the right".

This:

&(config->port)

means "take the address of the port member of the structure pointed to by config.

You don't want to take the address, you just want to assign to the member. That's just:

config->port = ...

This is the same as (*config).port, but nobody writes it like that since the arrow operator is so convenient.

Sign up to request clarification or add additional context in comments.

Comments

2
                read_str_from_config_line(value, &(config->name));

confg->name is declared as a char array, which naturally decays to a pointer to the first element (of type "pointer-to-char") if you omit the &:

                read_str_from_config_line(value, config->name);

Taking its address explicitly instead results in a pointer of type pointer-to-array-of-20-chars, which is not the same type as pointer-to-char - which is why you are getting an error. This is a common source of confusion in C; in general, you should not take the address of an array (although it does have legitimate use cases).

In this line:

            &(config->port) = read_int_from_config_line(value);

You are taking the address of the port member of the structure pointed at by config, which is a non-lvalue pointer. It looks like you are actually trying to assign to the member, so the line should read:

            config->port = read_int_from_config_line(value);

8 Comments

alright thanks. what if i would like to printf config.name? How should i do it ?
So in the whole code i'll use config->name instead of config.name for everything that i will do related will struct now?
@JimYong If you have a pointer to a struct, access its members using ->. So in your read_config_file function, use config->name, yes.
ok thanks. problem now is atconfig->port = read_int_from_config_line(value); it's not reading to config->port.
@JimYong You are not intialising the value pointer that you are passing as an argument. Are you compiling with warnings enabled? You should have got a warning about that I think.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.