Skip to content

Latest commit

 

History

History

0x0E-function_pointers

0x0E. C - Function pointers

0. What's my name mandatory

Write a function that prints a name.

  • Prototype: void print_name(char *name, void (*f)(char *));

1. If you spend too much time thinking about a thing, you'll never get it done mandatory

Write a function that executes a function given as a parameter on each element of an array.

  • Prototype: void array_iterator(int *array, size_t size, void (*action)(int));
  • where size is the size of the array
  • and action is a pointer to the function you need to use

2. To hell with circumstances; I create opportunities mandatory

Write a function that searches for an integer.

  • Prototype: int int_index(int *array, int size, int (*cmp)(int));
  • where size is the number of elements in the array array
  • cmp is a pointer to the function to be used to compare values
  • int_index returns the index of the first element for which the cmp function does not return 0
  • If no element matches, return -1
  • If size <= 0, return -1

3. A goal is not always meant to be reached, it often serves simply as something to aim at mandatory

Write a program that performs simple operations.

  • You are allowed to use the standard library
  • Usage: calc num1 operator num2
  • The program prints the result of the operation, followed by a new line
  • num1, num2 are integers. Use the atoi function to convert them to int
  • operator is one of the following:
    • +: addition
    • -: subtraction
    • *: multiplication
    • /: division
    • %: modulo
  • You can assume that the result of all operations can be stored in an int
  • if the number of arguments is wrong, print Error, followed by a new line, and exit with the status 98
  • if the operator is none of the above, print Error, followed by a new line, and exit with the status 99
  • if the user tries to divide (/ or %) by 0, print Error, followed by a new line, and exit with the status 100

This task requires that you create four different files.

3-calc.h

This file should contain all the function prototypes and data structures used by the program. You can use this structure:

4. Most hackers are young because young people tend to be adaptable. As long as you remain adaptable, you can always be a good hacker #advanced

Write a program that prints the opcodes of its own main function.

  • Usage: ./main number_of_bytes
  • Output format:
    • the opcodes should be printed in hexadecimal, lowercase
    • each opcode is two char long
    • listing ends with a new line
    • see example
  • You are allowed to use printf and atoi
  • You have to use atoi to convert the argument to an int
  • If the number of argument is not the correct one, print Error, followed by a new line, and exit with the status 1
  • If the number of bytes is negative, print Error, followed by a new line, and exit with the status 2
  • You do not have to compile with any flags

Note: if you want to translate your opcodes to assembly instructions, you can use, for instance udcli.