Dynamiczne lokowanie tablicy wskaźników – ktoś objaśni?

0

Cześć , dostałem takie zadanie dzisiaj do zrobienia. Nie rozumiem kompletnie co mam zrobić, ktoś chociaż wyjaśni ? Mam zrobić tablice wskaźników i 75% jej pamięci zalokować na hero ? i te 25% dać na jego wartości ? czyli te wskaźniki to Hero to ma być struck a te wartości mają być int ? Czy jak ?
Mam tylko takie polecenie.
Czytałem to ale w sumie nie pomaga mi bo nie wiem co zrobić
Tablice i wskaźniki, skomplikowana składnia

laborka.jpg

4

jak patrzę na to zadanie, to podsumuję to tak: prowadził ślepy kulawego.

2

To zadanie jest okropnie rozpisane :/ Jakby pisal jest "kolezka" z podstawowki co uczy sie programowac i chce zeby ktos mu to wytlumaczyl:

Ale mniej wiecej o cos takiego chodzi, dokoncz sobie z ekwipunkiem...

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

//musi byc compile time zeby uniknac warningow kompilatora
#define allocHeroes(heroes, size)                                       \
{                                                                       \
    heroes = (struct hero**)malloc((sizeof(struct hero*) * size));      \
    for (int i=0; i<size; i++) {                                        \
        heroes[i] = NULL;                                               \
    }                                                                   \
}   


struct hero {
    char name[25];
    unsigned int level;
} ;

struct equipment {
    int def;
    int atk;
};

struct heroEq {
    char name[25];
    unsigned int level;
    struct equipment eq;
};

void printAHero(int i, struct hero* hero) 
{
    printf("Hero(%d): %s, level %d\n", i, hero->name, hero->level);
}

void printHeroes(struct hero** heroes, int num) 
{
    for (int i=0; i<num; i++) {
        if (heroes[i] == NULL) {
            continue;
        }
        printAHero(i, heroes[i]);
    }
}

void createHero(struct hero** heroes, int idx, const char name[25], unsigned int level)
{
    heroes[idx] = (struct hero*)malloc(sizeof(struct hero));
    heroes[idx]->level = level;
    memcpy(heroes[idx]->name, name, 25);
}

//Mozesz tez zrobic to w postaci compiletime define ( w sumie caly ten kod mozesz xD )
void freeHeroes(struct hero** heroes, int num) 
{
    //Najpierw zwolnij pamiec dla wszystkich bohaterow
    for (int i=0; i<num; i++) {
        if (heroes[i] == NULL) {
            continue;
        }
        free(heroes[i]);
    }
    
    //teraz usun pamiec dla tablicy
    free(heroes);
}




int main()
{
    struct hero** heroes;
    
    //pobierz od uzytkownika rozmiar tablicy
    int size = 10;
    
    //pobierz index
    int idx = 10;
    
    if (idx+1 > size)  {
        return 1;
    }
    
    
    allocHeroes(heroes, size);
    createHero(heroes, idx, "Test", 0);
    printHeroes(heroes, size);
    return 0;
}
2

Zadanie jest bez sensu (edit: w C++). Ciężko oceniać po jednym przypadku, ale tutaj mogę z dużym prawdopodobieństwem stwierdzić, że ta osoba nie powinna uczyć ludzi C++. edit: po przeczytaniu tagów okazało się, że nie uczy. Uff

  • tablica wskaźników
  • sugestia ręcznego zwalniania pamięci

Tak czy inaczej, w ramach ćwiczenia napisałem coś co chyba jest zgodne z założeniami zadania... i nie urąga wszelkim standardom C++. edit: zostawiam, niech sobie leży.

#include <iostream>
#include <optional>

#include <boost/ptr_container/ptr_vector.hpp>

struct hero
{
    struct eq
    {
        int atk, def;
    };

    std::string name;
    int level;
    std::optional<eq> equipment;
};


std::ostream& operator<<(std::ostream& o, hero const& h)
{
    o << h.name << ":" << h.level;
    if(h.equipment)
        o << "(" << h.equipment->atk << "," << h.equipment->def << ")";
    return o;
}

int main()
{
    boost::ptr_vector<boost::nullable<hero>> heroes;
    int size;
    std::cin >> size;
    heroes.resize(size, nullptr);

    auto print_all = [&]{
        std::cout << std::string(80, '-') << '\n';
        for(int i = 0; i < heroes.size(); ++i) {
            if(heroes.is_null(i))
                continue;
            
            std::cout << i << ":" << heroes[i] << '\n';
        }
    };

    print_all();

    int idx, lvl;
    std::string name;
    std::cin >> idx >> name >> lvl;
    heroes.replace(idx, new hero{name, lvl});

    print_all();

    int atk, def;
    std::cin >> atk >> def;
    heroes[idx].equipment = hero::eq{atk, def};

    print_all();
}

https://wandbox.org/permlink/BSyeU9A6Goxbcvke

0

Okej dostałem takie zadanie teraz i próbuje je zrobić sam ale nie wiem jak zrobić c



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


struct person{

char *surname[];
unsigned int wiek;
int iloscPotomkow;
struct potomek *potomkowie[];
};

struct potomek{
char *surname[];
unsigned int wiek;
};

struct zrobPotomka(struct *person person)
{
    struct potomek** potomek;
      potomek[idx-1] = (struct person*)malloc(sizeof(struct person));
    potomek[idx-1]->level = person->level;
    memcpy(potomek[idx-1]->surname, persons[idx]->surname,persons[idx]->surname.sizeof()/"a".sizeof() );
    person->iloscPotomkow+;
    person->potomkowie[idx-1]=potomek;
    freePersons(person[idx]);

};

bool ifPersonExist(int i, struct person* person){

if (persons[i] == NULL){
    return true;
}
else
    return false;
}



void printAPerson(int i, struct person* person)
{
    printf("Person(%d): %s, level %d\n", i, person->name, person->level);
}


void printPersons(struct person** persons, int num)
{
    for (int i=0; i<num; i++) {
        if (persons[i] == NULL) {
            continue;
        }
        printAPerson(i, persons[i]);
    }
}

void createPersons(struct person** persons, int idx, const char *surname[], unsigned int level,int surnameLong)
{
    persons[idx] = (struct person*)malloc(sizeof(struct person));
    persons[idx]->level = level;
    memcpy(persons[idx]->surname, surname,surnameLong );
}

void freePersons(struct person** persons, int num)
{
    //Najpierw zwolnij pamiec dla wszystkich bohaterow
    for (int i=0; i<num; i++) {
        if (persons[i] == NULL) {
            continue;
        }
        free(persons[i]);
    }

    //teraz usun pamiec dla tablicy
    free(persons);
}


int main() {

struct person** persons;



char i;
 scanf("%c", &i);

char surname[100];
int age;
int idx;
int size;
printf("podaj ile chcesz stworzyć osób");
scanf(&size);
switch(i) {
    case 'a':

            printf("podaj indeks do utowrzenia osoby  ");
scanf("%d",idx);
            if(ifPersonExist(idx))
            {
                printf("podaj prosze nazwisko oraz wiek");
                scanf(&surname,&age);
                createPersons(person,idx,surname,age,surname.sizeof()/surname[0]);
            }
else
    printf("osoba o podanym in...");
 case 'c':


break;
 case 'p':
       printPersons(persons,size);
break;
 default:



}
return 0;
}

0

//musi byc compile time zeby uniknac warningow kompilatora
#define allocHeroes(heroes, size)
{
heroes = (struct hero**)malloc((sizeof(struct hero*) * size));
for (int i=0; i<size; i++) {
heroes[i] = NULL;
}
}

to jak to mniejwięcej ma być po usprawnieniu z
struct hero* heroAllocateArray(int size) { return (struct hero*)calloc(size, sizof(hero*)); }

1 użytkowników online, w tym zalogowanych: 0, gości: 1