function and dynamic alocate pointer to array by dzury

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct{
    char pismeno;
    unsigned int pocet;
}LETTER;

LETTER *countletters(char *path){
    FILE *f;
    f=fopen(path,"r");
    if(f==NULL) return NULL;
    char pism;
    LETTER *rola;
    int i;
    rola= malloc(25*sizeof(LETTER));
    rola[0].pismeno='a';
    rola[1].pismeno='b';
    rola[2].pismeno='c';
    rola[3].pismeno='d';
    rola[4].pismeno='e';
    rola[5].pismeno='f';
    rola[6].pismeno='g';
    rola[7].pismeno='h';
    rola[8].pismeno='i';
    rola[9].pismeno='j';
    rola[10].pismeno='k';
    rola[11].pismeno='l';
    rola[12].pismeno='m';
    rola[13].pismeno='n';
    rola[14].pismeno='o';
    rola[15].pismeno='p';
    rola[16].pismeno='q';
    rola[17].pismeno='r';
    rola[18].pismeno='s';
    rola[19].pismeno='t';
    rola[20].pismeno='u';
    rola[21].pismeno='v';
    rola[22].pismeno='x';
    rola[23].pismeno='y';
    rola[24].pismeno='z';
    while( ( pism = fgetc(f) ) != EOF)
    {   
        for (i=0;i<25;i++){        
         if (rola[i].pismeno==tolower(pism)) rola[i].pocet++;

        }   
    }
   
    return rola;
     fclose(f);
}

void printletters(LETTER *counter){
    int i;
 for (i=0;i<25;i++){
        printf("%c  %d\n",counter[i].pismeno,counter[i].pocet);
    }
    
}
int sort_letters(LETTER *counter)
{   
 int i ,x;
 LETTER pp;
 for (i=0;i<25;i++){
 for (x=0;x<25;x++){
 if (counter[x].pocet < counter[i].pocet)
 { pp=counter[x];
 counter[x]=counter[i];
 counter[i]=pp;

 }
 }
     }
for (i=0;i<25;i++){
        printf("%c  %d\n",counter[i].pismeno,counter[i].pocet);
    }
}

int main(int argc, char** argv) {
    LETTER *counter =countletters("test.txt");
    sort_letters(counter);
    return (EXIT_SUCCESS);
}



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

typedef struct{
   int rok;
    int day;
    int speed;
    int temperature;
}DATA;

void print(DATA *rola,int *n){
   int i;
for(i=0;i<*n;i++){
        printf("%d    %d    %2d    %d\n",rola[i].rok,rola[i].day,rola[i].speed,rola[i].temperature);
    }
}

void sort_temp(DATA *data, int *n){
    printf("\n  Sort to temperature\n");
    printf(" ROK   DAY   SPEED  TEMP\n");
    int i,j;
    DATA temp;
    for(i=0;i<*n;i++){
        for(j=0;j<*n;j++){
            if(data[i].temperature<data[j].temperature){
                temp=data[i];
                data[i]=data[j];
                data[j]=temp;
            }
        }
     }
     print(data,n);
}


void insert(DATA *data,int *n,int rok){ //podla daneho roku  zmenime prislusny riadok hodnot
    int i;    
    for(i=0;i<*n;i++){
        if(data[i].rok==rok) { printf("\nZadaj hodnoty speed a temperature\n");
                                scanf("%d %d\n",&data[i].speed,&data[i].temperature);
                        }
    }
    
}

DATA *rolaa(int *poc){
 DATA *rola;
 int i=0;
 rola=(DATA *)malloc(20*sizeof(DATA));
FILE *f;
    f=fopen("data.txt","r");
    if(f==NULL) { printf("Subor sa nepodarilo otvorit\n"); return NULL; }
   
       while(( fscanf(f,"%d%d%d%d",&rola[i].rok,&rola[i].day,&rola[i].speed,&rola[i].temperature))!=EOF){
                i++;
           }
    *poc=i;
    return rola;
}

int main(int argc, char** argv) {


    int *poc;  //premenna pocet riadkov naplni sa vo funkcii rolaa(poc));
    DATA *rola;
    rola=rolaa(poc);
    if(rola==NULL) return EXIT_FAILURE;
    print(rola,poc);
   sort_temp(rola,poc);

   
    int rok;
    puts("\nZadaj rok\n");
    scanf("%d",&rok);
    insert(rola,poc,rok);
    sort_temp(rola,poc);
    free(rola);
    
    return (EXIT_SUCCESS);
}

Last edited on
51
52
    return rola; //function terminates here
     fclose(f); //it could never be reached, leak. 


1
2
3
    LETTER *counter =countletters("test.txt");
    sort_letters(counter);
    return (EXIT_SUCCESS); //you forgot to free(counter) 


Also, fix your indentation
Topic archived. No new replies allowed.