Arrays in C


Here a working example for an array:

#include 
#include 

typedef struct myStruct {
 int x;
 int y;
} tMyStruct;


void displayString(const char* myString) {
 printf("%s\r\n", myString);
}

void displayIntArray(int myIntArray[], int myLength) {
 for (int i = 0; i < myLength; i++)
 {
 printf("%d\r\n", myIntArray[i]);
 }
}

void displayStractArray(tMyStruct* myStructArray, int lengthOfStruct) {

for (int i = 0; i < lengthOfStruct; i++)
 {
  printf("%d\r\n", myStructArray[i].x);
  printf("%d\r\n", myStructArray[i].y);
 }
}

void maszTablice(int* tabOfMyIndexes, int sizeOftabOfMyIndexes, int* returnTab, int sizeOfReturnTab) {
	for (int i = 0; i < sizeOftabOfMyIndexes; i++)
	{
		printf("%d\r\n", tabOfMyIndexes[i]);  // wyswietla co pszyjszuo
	}

	for (int i = 0; i < sizeOfReturnTab; i++)
	{
		returnTab[i] = 0xff;  // nadpisanie tabeli zwrotnej
	}
}

int main() {
 displayString("jakis huj");

 int myIntArray[6] = {10, 20, 30, 40, 50, 60};
 int sizeOfArray = sizeof(myIntArray)/sizeof(myIntArray[0]);
 displayIntArray(myIntArray, sizeOfArray);

 tMyStruct myStructArray[6];
 myStructArray[0].x = 10;
 myStructArray[0].y = 20;
 myStructArray[1].x = 11;
 myStructArray[1].y = 21;
 myStructArray[2].x = 12;
 myStructArray[2].y = 22;
 int sizeOfStructArray = sizeof(myStructArray)/sizeof(*myStructArray);
 displayStractArray(myStructArray, sizeOfStructArray);

 // passing of fixed length array as data source and another array as return array
 int tabOfMyIndexes[8] = {};
 int sizeOftabOfMyIndexes = sizeof(tabOfMyIndexes) / sizeof(*tabOfMyIndexes);
 int returnTab[8] = {};
 int sizeOfReturnTab = sizeof(returnTab) / sizeof(*returnTab);
 maszTablice(tabOfMyIndexes, sizeOftabOfMyIndexes, returnTab, sizeOfReturnTab);

 myCout("sprawdzenie sizeOfReturnTab[]: " << endl);
 for (int i = 0; i < sizeOfReturnTab; i++)
 {
	myCout(returnTab[i] << endl);
 }
 return 0;
}

drugie podejscie. proste jak jebanie. rzadnego maloka ani hujuf-mujuf.

void passingArray(const int myArray[], const int itsSize) {
	for (int i = 0; i < itsSize; i++) {
		printf("[%d]: %d\n", i, myArray[i]);
	}
}

void case4() {
	const int itsSize = 6;
	int myArray[] = {1, 2, 3, 4, 5, 6};
	passingArray(myArray, itsSize);
}

2 responses to “Arrays in C”

Leave a Reply

Your email address will not be published. Required fields are marked *