Помощь - Поиск - Пользователи - Календарь
Полная версия: структуры в Си
Форум «Всё о Паскале» > Современный Паскаль и другие языки > Ада и другие языки
-Night-
Здравствуйте.
Помогите разобраться. Есть структура описывающая книгу. Далее даётся имя автора и надо удалить из массива соответствующую структуру... Я что делаю, бсчитываю имя автора па потом вызываю функцию и ищу где имя считанное совпадает с именем автоора в структуре, дело в том, что почему то равенство совершенно не проверяется....что делать не знаю....вывожу промежуточные значения вижу что размер строки один, значения одинаковые, а рвенство неверно, что делать?
Исходник

#ifndef _HEADER_H
#define _HEADER_H

#define TRUE 1;
#define FALSE 0;

typedef unsigned int UI;
typedef unsigned char UC;

typedef struct{
UC *author;
UC *name;
UC *publ;
UI year;
UI count;
} str_lib;

extern int count = 1;
#endif

----------------------------------------

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

#define OK_EXIT 0

void func_print(str_lib lib)
{
printf("%s\n ", &lib.author);
printf("%s\n ", &lib.name);
printf("%s\n ", &lib.publ);
printf("%d\n ", &lib.year);
printf("%d\n ", &lib.count);
}

void func_struct_del(str_lib *arr, char *author)
{
int local_count = 1;
int count_str_len = 0;
int bool = 1;
char *fin_author;
char *isk_author;
int len_fin_author;
int len_isk_author;

printf("%s\n", "OK in func...");

fin_author = (char *)&arr[1].author;
isk_author = (char *)&author;

len_fin_author = strlen(fin_author);
len_isk_author = strlen(isk_author);

printf("%c");

/*
for (count_str_len = 0; count_str_len <= len_fin_author; count_str_len++)
{

// printf("%c", &arr[count_str_len]);
if (&arr->author[count_str_len] == &author[count_str_len])
{
bool = 1;
}
else bool = 0;

}

printf("%d", bool);

}
*/
int main (void)
{
char *command;
UC *author;
str_lib *arr;

arr = (str_lib *) malloc(sizeof(str_lib));
while (count <= 3)
{
printf("%s %d %s\n", "--------- input struct ", count,"begin---------");
scanf("%s\n", &arr[count].author);
scanf("%s\n", &arr[count].name);
scanf("%s\n", &arr[count].publ);
scanf("%d\n", &arr[count].year);
scanf("%d", &arr[count].count);
printf("%s %d %s\n", "---------input struct ", count,"end---------");
count++;
}
printf("\n");

for (count = 1; count <= 3; count++)
{
printf("%s %d %s\n", "--------- output struct ", count,"begin---------");
func_print(arr[count]);
printf("%s %d %s\n", "--------- output struct ", count,"end---------");
}

printf("\n");
printf("%s\n", "change function to do with struct");
printf("%s", " :");
scanf("%s %s", &command, &author);

if (&command == "delete")
{
printf("%s %s %s\n", "deleting book by", &author, "...");
func_struct_del(arr, author);
}
else
{
printf("%s %s %s\n", "deleting book by", &author, "...");
func_struct_del(arr, author);
}

return OK_EXIT;
}
volvo
Смотри:
arr = (str_lib *) malloc(sizeof(str_lib)); // <-- Здесь ты выделил память под структуру ...

// Но в структуре у тебя нет места под строку, есть - только под УКАЗАТЕЛЬ на строку
// Так что прежде, чем делать то, что ты делаешь ниже, надо бы выделить память под сами строки:

while (count <= 3)
{
printf("%s %d %s\n", "--------- input struct ", count,"begin---------");
scanf("%s\n", &arr[count].author);
scanf("%s\n", &arr[count].name);
scanf("%s\n", &arr[count].publ);
scanf("%d\n", &arr[count].year);
scanf("%d", &arr[count].count);
printf("%s %d %s\n", "---------input struct ", count,"end---------");
count++;
}

или в описании структуры заменить указатель на строку символьным буфером ...

Это было первое... Второе: строки не сравниваются так, как ты делаешь это... Есть файл string.h, и в нем описана функция strcmp, которая занимается сравнением строк.
Это текстовая версия — только основной контент. Для просмотра полной версии этой страницы, пожалуйста, нажмите сюда.