Last active
August 29, 2015 14:15
-
-
Save ricardoamaro/ac7a9de2b467c825dc8c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* EXEMPLOS EM C PARA: | |
- Dynamic allocation (malloc) - Alocação de memória | |
- Linked lists - Listas | |
- Function arguments by reference - Argumentos de função por referẽncia | |
- Recursion - Recursão | |
*/ | |
//------------READ FILES----------- | |
#include <stdio.h> | |
#define MAXSTR 255 | |
int main(int argc, char *argv[]) | |
{ | |
FILE *f; | |
char str[MAXSTR]; | |
f=fopen(argv[1],"rt"); | |
if(f!=NULL) | |
{ | |
while(fgets(str,MAXSTR,f)!=NULL) { | |
printf("linha: %s",str); | |
} | |
} | |
fclose(f); | |
return 0; | |
} | |
//-------------Dynamic allocation (malloc)------------------------------ | |
#include <stdio.h> | |
typedef struct { | |
int x; | |
int y; | |
} point; | |
int main() { | |
point * mypoint; | |
mypoint = malloc(sizeof(point)); | |
mypoint->x = 10; | |
mypoint->y =5 ; | |
printf("mypoint coordinates: %d, %d\n", mypoint->x, mypoint->y); | |
free(mypoint); | |
return 0; | |
} | |
//-------------Linked lists--------------------------------- | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
int val; | |
struct node * next; | |
} node_t; | |
void print_list(node_t * head) { | |
node_t * current = head; | |
while (current != NULL) { | |
printf("%d\n", current->val); | |
current = current->next; | |
} | |
} | |
int pop(node_t ** head) { | |
int retval = -1; | |
node_t * next_node = NULL; | |
if (*head == NULL) { | |
return -1; | |
} | |
next_node = (*head)->next; | |
retval = (*head)->val; | |
free(*head); | |
*head = next_node; | |
return retval; | |
} | |
int remove_by_value(node_t ** head, int val) { | |
int i = 0; | |
int retval = -1; | |
node_t * current = *head; | |
node_t * temp_node = NULL; | |
if ((*head)->val == val) { | |
return pop(head); | |
} | |
while (current->next->val != val) { | |
if (current->next == NULL) { | |
return -1; | |
} | |
current = current->next; | |
} | |
temp_node = current->next; | |
retval = temp_node->val; | |
current->next = temp_node->next; | |
free(temp_node); | |
return retval; | |
} | |
int main() { | |
node_t * test_list = malloc(sizeof(node_t)); | |
test_list->val = 1; | |
test_list->next = malloc(sizeof(node_t)); | |
test_list->next->val = 2; | |
test_list->next->next = malloc(sizeof(node_t)); | |
test_list->next->next->val = 3; | |
test_list->next->next->next = malloc(sizeof(node_t)); | |
test_list->next->next->next->val = 4; | |
test_list->next->next->next->next = NULL; | |
remove_by_value(&test_list, 3); | |
print_list(test_list); | |
} | |
//--------------Function arguments by reference----------- | |
#include <stdio.h> | |
typedef struct { | |
char * name; | |
int age; | |
} person; | |
/* function declaration */ | |
void birthday(person * p); | |
void birthday(person * p){ | |
(*p).age += 1; | |
} | |
int main() { | |
person john; | |
john.name = "John"; | |
john.age = 27; | |
printf("%s is %d years old.\n", john.name, john.age); | |
birthday(&john); | |
printf("Happy birthday! %s is now %d years old.\n", john.name, john.age); | |
return 0; | |
} | |
//--------------Recursion-------------------------------------- | |
#include <stdio.h> | |
int factorial(int number); | |
int main() { | |
/* testing code */ | |
printf("1! = %i\n", factorial(1)); | |
printf("3! = %i\n", factorial(3)); | |
printf("5! = %i\n", factorial(5)); | |
} | |
int factorial(int number){ | |
int f = number; | |
if(number > 1){ | |
f *= factorial(number-1); | |
} | |
return f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment