How do you pass a struct pointer through a function? - c

#define WIDTH_DATA 20
typedef struct
{
float inhalt;
unsigned int nr;
int status;
} Paket;
typedef struct
{
Paket Eingang[WIDTH_DATA];
int laenge;
float Ablage_A[WIDTH_DATA];
float Ablage_B[WIDTH_DATA];
} Data_t;
#include <stdio.h>
int pruefe_status(int i1)
{
if (i1==0)
{
return 0;
}
else
{
return 1;
}
}
int vergleiche(unsigned int i1, unsigned int i2)
{
if (i1<i2)
{
return 1;
}
else
{
return 0;
}
}
int verteile_Pakete(Data_t *dv){
int counterA=0,counterB=0;
for (int i=0; i<WIDTH_DATA; i++)
{
if (pruefe_status(dv->Eingang[i].status)==0)
{
if(vergleiche(dv->Eingang[i].nr,5000))
{
dv->Ablage_A[counterA] = dv->Eingang[i].inhalt;
counterA++;
}
else{
dv->Ablage_B[counterB] = dv->Eingang[i].inhalt;
counterB++;
}
}
}
return counterB + counterA;
}
int main(void)
{
// Beispiel für testdv1:
Data_t testdv1 = {{{12.5, 6067, 1}, {45.7, 3002, 0}, {56.0, 3456, 1}}, 3, {0}, {0}};
Data_t testdv2 = {{{12.5, 6067, 0}, {45.7, 3002, 1}, {56.0, 3456, 1}}, 3, {0}, {0}};
Data_t testdv3 = {{{12.5, 6067, 0}, {45.7, 7002, 0}, {56.0, 6456, 0}, {13.5, 6067, 0}, {1.7, 7772, 0}, {156.0, 5656, 0}}, 6, {0}, {0}};
int Anzahl_pakete1=0;
int Anzahl_pakete2=0;
int Anzahl_pakete3=0;
Anzahl_pakete1 = verteile_Pakete(&testdv1);
Anzahl_pakete2 = verteile_Pakete(&testdv2);
Anzahl_pakete3 = verteile_Pakete(&testdv3);
printf("test 1:\nAnzahl bearbeitenden Pakete:%f\n",Anzahl_pakete1);
for (int i=0;i<10;i++)
{
printf("AblageA %d: %f\nAblageB %d: %f\n",i,testdv1.Ablage_A[i],i,testdv1.Ablage_B[i]);
}
printf("test 2:\nAnzahl bearbeitenden Pakete:%f\n",Anzahl_pakete2);
for (int i=0;i<10;i++)
{
printf("AblageA %d: %f\nAblageB %d: %f\n",i,testdv2.Ablage_A[i],i,testdv2.Ablage_B[i]);
}
printf("test 3:\nAnzahl bearbeitenden Pakete:%f\n",Anzahl_pakete3);
for (int i=0;i<10;i++)
{
printf("AblageA %d: %f\nAblageB %d: %f\n",i,testdv3.Ablage_A[i],i,testdv3.Ablage_B[i]);
}
return(0);
}
Why is the data in testdv1 or 2 or 3 not going in the function verteile_Pakete?.
I tried to pass the address of these test variables to the function as a pointer but the values get lost and don't get through when I try to debug. So everything comes out as 0.
How can I solve this?

When you look at the compiler output (https://godbolt.org/z/jM43PY916) it warns you that you try to output the integer count as float.
Replace %f with %d in your format strings for integers and it works.

Related

Copy array's data with memmove function

I wrote my implementation of vector ( dynamicly growing array) for integer type. And I was faced with a problem when using memmove and memcpy functions for data reallocation goals. Memmmove and memcpy functions work as not expected. So i replaced it with for and now it works properly. But what is the why memove works this way ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct vector {
int v_size;
int v_length;
int* v_data;
} vector;
typedef vector *vect;
vect
new_vector(int size) {
vect tmp = (vector*)malloc(sizeof(vector));
tmp->v_size = size;
tmp->v_length = 0;
tmp->v_data = (int*)malloc(sizeof(int) * size);
return tmp;
}
void
add_velem(vect tmp, int elem) {
if(tmp->v_length != tmp->v_size) {
tmp->v_data[tmp->v_length] = elem;
tmp->v_length += 1;
} else {
tmp->v_size = tmp->v_size * 2;
tmp->v_length += 1;
int *new_vector = (int*)malloc(sizeof(int) * tmp->v_size);
memmove(new_vector, tmp->v_data, tmp->v_length); // GOT INPUT LIKE THIS:
// 500, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
// ALOT OF ZERO
/*
for(int i = 0; i < tmp->v_length; i++) {
new_vector[i] = tmp->v_data[i];
}*/ // BUT GOT RIGHT INPUT WHEN USING FOR. WHAT IS THE REASON?
free(tmp->v_data);
tmp->v_data = new_vector;
tmp->v_data[tmp->v_length - 1] = elem;
}
return;
}
int
get_vlength(vect tmp) {
return tmp->v_length;
}
int
get_vsize(vect tmp) {
return tmp->v_size;
}
int
get_velem(vect tmp, int elem) {
if(tmp->v_data == NULL) {
fprintf(stderr, "Index out of range!\n");
}else if(elem >= tmp->v_length) {
fprintf(stderr, "Index is out of range!\n");
return -1;
}
return tmp->v_data[elem];
}
void
delete_vector(vect tmp) {
free(tmp->v_data);
tmp->v_data = NULL;
free(tmp);
tmp = NULL;
}
int
main(void) {
vect example = new_vector(10);
printf("lenth of vector is %d\n", get_vlength(example));
add_velem(example, 500);
printf("element %d is pushed into vector\n", 500);
printf("size of vector is %d and\nlength of vector is %d\n", get_vsize(example), get_vlength(example));
int velem = get_velem(example, 0);
printf("elem 0 of vector is %d\n", velem);
for(int i = 1; i < 30; i++) {
add_velem(example, i);
}
printf("length of vector is %d now\n", get_vlength(example));
for(int i = 0; i < get_vlength(example); i++) {
printf("%d, ", get_velem(example, i));
}
delete_vector(example);
return 0;
}

Checking for numbers within arrays in C

The function below is looking through Order1-3 and looks if any of the elements are within each of the 3 column values of each of the 8 rows inside Winning_order. So in this case Order1 values are within the first row of Winning_order so it comes out as true as well as Order2. However Order2 is not a valid output for the program, how can I modify the iterating function below so that it checks to be true. Order3 is meant to be a false as well since its not within Winning_order. The code has been gotten from the answer of this issue issue.
#include <stdio.h>
// Iterating function
int match_arrays(int *arr1, int *arr2, int len)
{
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
// Main function
int main(void)
{
int Order1[3] = {1,5,9};
int Order2[4] = {1,2,5,3};
int Order3[3] = {4,4,4};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Expected Output
Order 1 Order 2
This matching method will output
Order2Order1
Could you explain some detail why Order2 will be true?
#include <stdio.h>
int match_arrays(int* arr_order, int* arr_win, int len_order, int len_win)
{
int idx = 0;
int err = 0;
for (int i = 0; i < len_order; i++) {
if (arr_order[i] != arr_win[idx]) {
err++;
if (err > len_order - len_win) {
return 0;
}
} else {
idx++;
if (idx == len_win)
{
return 1;
}
}
}
return 1;
}
int main()
{
int Order1[3] = {1, 5, 9};
int Order2[4] = {1, 2, 5, 3};
int Order3[3] = {4, 4, 4};
int Winning_order[8][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 4, 7}, {2, 5, 8}, {3, 6, 9}, {1, 5, 9}, {3, 5, 7}};
for (int i = 0; i < 8; i++) {
if (match_arrays(Order1, Winning_order[i], 3, 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 4, 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3, 3)) {
printf("Order3");
}
}
return 0;
}

Movement issues, skipping fields

I'm making a game, where you (the player) are represented by a number in a 2D matrix. The problem is that when I move the player, it skips fields, and does not respond very well. The movement is very inaccurate. If you could run the program, you could get a better understanding of the issue. How can I fix this problem?
#include <stdio.h>
#include <windows.h>
#include <time.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock())
;
}
int map[5][5] = {{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 0}};
int mUpdate(int x)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
printf("%d ", map[i][j]);
}
printf("\n");
}
}
int mRefresh(int x)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (map[i][j] > 0)
{
map[i][j] = 0;
}
}
}
}
int main()
{
int c;
int i, j;
int p_num;
printf("Hey! Choose a number from 1-9 that will represent you!: ");
scanf("%d", &p_num);
printf("Choose a starting position by inputting two coordinates from 1-4!: ");
scanf("%d %d", &i, &j);
printf("\nVery well, this is your starting position!\n");
map[i][j] = p_num;
mUpdate(map[i][j]);
printf("\nYou are now the number %d !\n", p_num);
printf("\nPlease wait for the map to load!");
printf("\nMap loading will start soon!");
delay(3000);
for (c = 0; c < 5; c++)
{
printf("\nLoading.");
delay(500);
system("cls");
printf("\nLoading..");
delay(500);
system("cls");
printf("\nLoading...");
delay(500);
system("cls");
}
printf("Map loaded!\n");
printf("\nUse arrow keys to move thru the matrix!\n");
mUpdate(map[i][j]);
int game_running = true;
while (game_running = true)
{
if (GetAsyncKeyState(VK_DOWN))
{
// delay(200);
map[i++][j] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_UP))
{
// delay(200);
map[i--][j] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_RIGHT))
{
// delay(1000);
map[i][j++] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
else if (GetAsyncKeyState(VK_LEFT))
{
// delay(200);
map[i][j--] = p_num;
system("cls");
mUpdate(map[i][j]);
mRefresh(map[i][j]);
}
}
system("cls");
}

Ccrabble C program compiling but is not allowing the user to execute the file

The user is supposed to be prompted with several letters that they are to decode and then input their word which returns a score for that word.
I can get the program to compile but the program does not actually run. So I am not entirely sure where the issue lies within my code.I am relatively new at this.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define num_letters_input 7
int main()
{
const int alphabet_count[26] = {8, 2, 2, 4, 12, 2, 3, 2, 9, 1, 1, 4, 2, 6, 8, 2, 1, 6, 4, 6, 4, 2, 2, 1, 2, 1};
const int alphabet_value[26] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
int letter_set[26];
int size_letter_set = 26;
void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {
int random_a = rand() % 26;
int random_b = rand() % (alphabet_count[random_a]);
letter_set[random_a] = random_b;
printf("Your letters are: ");
for(int i=0; i < size_letter_set; i++) {
if(letter_set[i])
{
int num = letter_set[i];
while(num--)
printf("%c ", i+97);
}
}
printf("\n");
}
int read_word(char word[], int max_size_word) {
printf("Enter your word: ");
scanf("%s", word);
printf("%s \n", word);
int size_word = strlen(word);
if(size_word > max_size_word)
read_word(word, max_size_word);
return size_word;
}
bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {
int count_array[26];
for(int i=0; i<size_word; i++) {
count_array[word[i]-97]++;
}
for(int i=0; i<size_letter_set; i++) {
if(count_array[i] <= letter_set[i])
continue;
else {
return false;
}
return true;
}
int compute_word_value(char word[], int size_word) {
int word_value = 0;
for(int i=0; i<size_word; i++) {
word_value = word_value + alphabet_value[word[i]-97];
}
return word_value;
}
int main(void) {
int max_size_word = 7;
int size_word, word_value;
bool validity = false;
char word[95];
printf("This program plays the game of scrabble.\n");
for(int i=0; i < size_letter_set; i++) {
letter_set[i] = 0;
}
generate_letter_set(letter_set, size_letter_set, num_letters_input);
while(!validity) {
size_word = read_word(word, max_size_word);
validity = check_word(word, size_word, letter_set, size_letter_set);
if(!validity)
{
printf("The word is not valid. Use your letters: ");
for(int i=0; i < size_letter_set; i++) {
if(letter_set[i])
{
int num = letter_set[i];
while(num--) {
printf("%c ", i+97);
}
}
printf("\n");
}
}
printf("The value of your word is: ");
word_value = compute_word_value(word, size_word);
printf("%d", word_value);
printf("Thank you for playing.");
}
}
}
return 0;
}
It may be somewhat different from what you intend,
I think it will be useful to start with the following modifications.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define num_letters_input 7
const int alphabet_count[26] = {8,2,2,4,12,2,3,2,9,1,1,4,2,6,8,2, 1,6,4,6,4,2,2,1,2, 1};
const int alphabet_value[26] = {1,3,3,2, 1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
void generate_letter_set(int letter_set[], int size_letter_set, int num_letters) {
printf("Your letters are: ");
for(int i = 0; i < num_letters; i++){
int random_a = rand() % 26;
int random_b = rand() % alphabet_count[random_a];
letter_set[random_a] = random_b;
}
for(int i = 0; i < size_letter_set; ++i){
if(letter_set[i]){
int num = letter_set[i];
while(num--)
printf("%c ", 'a' + i);
}
}
printf("\n");
}
int read_word(char word[], int max_size_word) {
int size_word = 0;
do {
printf("Enter your word: ");
scanf("%s", word);
size_word = strlen(word);
} while(size_word > max_size_word);
return size_word;
}
bool check_word(char word[], int size_word, int letter_set[], int size_letter_set) {
int count_array[26] = {0};
for(int i = 0; i < size_word; i++) {
count_array[word[i]-'a']++;
}
for(int i=0; i < size_letter_set; i++) {
if(count_array[i] <= letter_set[i])
continue;
else
return false;
}
return true;
}
int compute_word_value(char word[], int size_word) {
int word_value = 0;
for(int i=0; i<size_word; i++) {
word_value += alphabet_value[word[i]-97];
}
return word_value;
}
int main(void){
int letter_set[26] = {0};
int size_letter_set = 26;
int max_size_word = num_letters_input;
int size_word, word_value;
bool validity = false;
char word[96];
srand(time(NULL));
printf("This program plays the game of scrabble.\n");
generate_letter_set(letter_set, size_letter_set, num_letters_input);
while(!validity) {
size_word = read_word(word, max_size_word);
validity = check_word(word, size_word, letter_set, size_letter_set);
if(!validity){
printf("The word is not valid.\nUse your letters: ");
}
}
printf("The value of your word is: ");
word_value = compute_word_value(word, size_word);
printf("%d\n", word_value);
printf("Thank you for playing.\n");
return 0;
}

How to add adjacent array items in c with pointers?

#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (int i=0; i<sizeof(num1); i++) {
final[i] = num1[i] + num2[i];
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
So, I did the above without the pointers. But with pointers, here is my attempt: I'm still new to pointers, and I'm playing with different practice problems.
#include <stdio.h>
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5];
for (; *num1 != '\0'; *num1++) {
*final = *num1 + *num2;
}
for (int c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}
void main() {
add_adjacents();
}
The following does the trick:
void add_adjacents() {
int num1[5] = {1, 2, 3, 4, 5};
int num2[5] = {10, 20, 30, 40, 50};
int final[5], c;
int *n1= num1, *n2=num2, *f=final;
for (; n1<&num1[5]; ) {
*f++ = *n1++ + *n2++;
}
for (c=0; c<sizeof(final)/sizeof(final[0]); c++) {
printf("%d\n", final[c]);
}
}

Resources