ALT Linux Community general discussions
 help / color / mirror / Atom feed
From: Alexey Rusakov <ktirf@altlinux.ru>
To: community@altlinux.ru
Subject: Re: [Comm] send pointer on struct to function in C
Date: Tue, 15 Mar 2005 00:11:09 +0300
Message-ID: <4235FDED.20506@altlinux.ru> (raw)
In-Reply-To: <4235C282.5000701@ricom.ru>

[-- Attachment #1: Type: text/plain, Size: 2423 bytes --]

Alexey Morsov wrote:

>>> {"user", "vasya"},
>>> {"host", "vasya@com.ru")
>>>      
>>>
>>А здесь не помешало бы либо добавить , { NULL, NULL }, а в функции 
>>проверять на NULLовость ну например name, либо передавать в функцию 
>>отдельным параметром размер массива (в примерах этот параметр не использую).
>>    
>>
>Хм.... ну если вы имеет ввиду типа для определения конца массива при его 
>переборе.... так оно вроде и работает как есть
>Т.е. для текущей структуры конструкция вида
>
>while (cmd[i].user != NULL )
>   printf("%s\n", cmd[i++].host);
>
>вполне себе работает  :)
>  
>
Кхм. Вам крупно повезло.

>Тут наверное лучше пояснить на живом примере так сказать.
>Структура
>typedef struct {
>    const char *name;
>    char *val;
>} Options;
>
>инициализация
>Options cmd[] = {
>    {"squidlog", "access.log"},      // path to squid access.log file
>    {"config", "trafficd.conf"}   // path to trafficd conf file
>};
>
>
>функция
>
>char *OptionGetVal(Options *opt, char *name) {
>  
>
Здесь лучше const char *name. Просто для порядка.

>    int i=0;
>    while (opt[i].name != NULL ) {
>       if (!strcmp(name,opt[i].name))
>          return(opt[i].val);
>       i++;
>    }
>    return NULL;
>}
>
>вызов ее такой
>
>printf("squidlog=%s\n", OptionGetVal(cmd,"squidlog"));
>
>при компиляции имею:
>
>In file included from common.c:1,
>                  from main.c:2:
>trafficd.h:23: error: conflicting types for `Options'
>trafficd.h:23: error: previous declaration of `Options'
>trafficd.h:32: error: conflicting types for `OptionsGetVal'
>trafficd.h:32: error: previous declaration of `OptionsGetVal'
>
> >_< Вот я понимаю что я тут как-то не прав? Но где именно я не прав?
>  
>
Что-то вы не договариваете. Я пришпилил к этому письму файлик - он 
прекрасно компилируется через 'gcc-3.4 -o test test.c'.

-- 
  Alexey "Ktirf" Rusakov

[-- Attachment #2: test.c --]
[-- Type: text/plain, Size: 477 bytes --]

#include <stdio.h>

typedef struct {
    const char *name;
    char *val;
} Options;

Options cmd[] = {
    {"squidlog", "access.log"},
    {"config", "trafficd.conf"},
    {NULL, NULL}
};

char *OptionGetVal(Options *opt, const char *name) {
    int i=0;
    while (opt[i].name) {
       if (!strcmp(name,opt[i].name))
          return(opt[i].val);
       i++;
    }
    return NULL;
}

int main() {
    printf("squidlog=%s\n", OptionGetVal(cmd, "squidlog"));
    return 0;
}

  parent reply	other threads:[~2005-03-14 21:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-14 14:58 Alexey Morsov
2005-03-14 16:01 ` Alexandr R. Ogurtzoff
2005-03-14 16:46   ` Alexey Morsov
2005-03-14 16:50   ` Alexey Morsov
2005-03-14 16:51   ` Alexandr R. Ogurtzoff
2005-03-14 17:00     ` Alexey Morsov
2005-03-14 16:27 ` Alexey Rusakov
2005-03-14 16:57   ` Alexey Morsov
2005-03-14 21:00     ` Alexandr R. Ogurtzoff
2005-03-14 21:11     ` Alexey Rusakov [this message]
2005-03-15  8:01       ` Alexey Morsov
2005-03-15  8:20         ` Denis S. Filimonov
2005-03-15  8:38           ` Alexey Morsov
2005-03-15 11:01             ` Alexey Rusakov
2005-03-15 11:34               ` Alexey Morsov
2005-03-15 11:50                 ` Alexey Rusakov
2005-03-15  8:20         ` Michael Holzman
2005-03-14 21:23     ` Alexandr R. Ogurtzoff

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4235FDED.20506@altlinux.ru \
    --to=ktirf@altlinux.ru \
    --cc=community@altlinux.ru \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

ALT Linux Community general discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/community/0 community/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 community community/ http://lore.altlinux.org/community \
		mandrake-russian@linuxteam.iplabs.ru community@lists.altlinux.org community@lists.altlinux.ru community@lists.altlinux.com
	public-inbox-index community

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.community


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git