Skip to content

Instantly share code, notes, and snippets.

@alexprivalov
Created January 31, 2015 18:21
Show Gist options
  • Save alexprivalov/6970b24aa70ada10879f to your computer and use it in GitHub Desktop.
Save alexprivalov/6970b24aa70ada10879f to your computer and use it in GitHub Desktop.
for test task
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALIAS_QUANTITY 10
struct aliasID
{
char* alias; /* '\0'-terminated C string */
int specific_id;
};
/* How many structures should be pointed to by the array
*/
int GetNumberOfAliases(void)
{
return ALIAS_QUANTITY;
}
/* Get a pointer to the next structure. The structure itself
* will be filled with data.
* Caller is responsible for the cleanup of the returned structure
* and its content. The latter are allocated in
* dynamic memory.
*/
struct aliasID * GetNextAlias(void)
{
struct aliasID *p_ret = (struct aliasID *)malloc(sizeof(struct aliasID));
if (NULL != p_ret)
{
//get data from somewhere and fill up struct
int i = rand()%1000; //reduce order of random digit
char *p_str = (char*)malloc(sizeof(char)*5); //magic number 5: 4 symbols for random digit and 1 for terminate symbol
itoa(i, p_str, 10); //I know that this function is not part of ANSI C, using it just for sample
p_ret->specific_id = i;
p_ret->alias = p_str;
}
return p_ret;
}
/* Get a pointer to the array of integers.
* Caller is responsible for the cleanup of the returned array
*/
int * GetSomeIDs(struct aliasID*** aliasIDs, int* numberOfAliases)
{
int *p_ret = NULL;
printf("address after0: %x\n", aliasIDs);
do
{
int i = 0;
int *p_tmp = NULL;
*numberOfAliases = GetNumberOfAliases();
p_tmp = p_ret = (int*)malloc(sizeof(int)* (*numberOfAliases));
if(NULL == p_ret)
{
//error occurs, do something usefull
break;
}
//alocate memory for array of pointers
*aliasIDs = (struct aliasID *)malloc(sizeof(struct aliasID*) * (*numberOfAliases));
memset(*aliasIDs, 0, *numberOfAliases);
printf("address after1: %x\n", aliasIDs);
if(NULL == aliasIDs)
{
//error occurs, do something usefull
break;
}
for (; i < *numberOfAliases; i++)
{
struct aliasID *p_alias = GetNextAlias();
if (NULL != p_alias)
{
*p_tmp = p_alias->specific_id;
(*aliasIDs)[i] = p_alias;
} else
{
*p_tmp = 0; //writes 0 if we can't get new struct
}
p_tmp++;
}
} while(0);
return p_ret;
}
void printAllAndCleanUp()
{
int i = 0;
struct aliasID **p_aliasID_arr = NULL;
int numberOfAlias = 0;
int *p_arr = NULL;
p_arr = GetSomeIDs(&p_aliasID_arr, &numberOfAlias);
printf("Content of integer array:\n");
for (; i < numberOfAlias; i++)
{
printf("%d ", *(p_arr+i));
}
printf("\nContent of struct array:\n");
i = 0;
for (; i < numberOfAlias; i++)
{
printf("alias id = %d, alias string = '%s'\n", p_aliasID_arr[i]->specific_id, p_aliasID_arr[i]->alias);
}
//clean up
free(p_arr);
i = 0;
for (; i < numberOfAlias; i++)
{
free(p_aliasID_arr[i]->alias);
free(p_aliasID_arr[i]);
}
free(p_aliasID_arr);
}
int main(int argc, char ** argv)
{
printAllAndCleanUp();
printf("\nAll ok!\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment