Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Structures and Pointers

Programming with structures


and pointers

Objective: The aim of this test is to code a simple


data structure problem in C
Contents:
Guidelines

Instructions

Note!

Hints

Output Expected

Submission

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
1
Guidelines
- You must edit the file sample.c provided to you and follow
the instructions as specified in this document.

- Only header files mentioned in the sample.c file are


allowed.

- Function pointers f1, f2 and f3 must always point to a


function and then be executed as function pointers. You can
use functions elsewhere as needed.

- Your functions should not quit unexpectedly (segmentation


fault, bus error, double free, etc) apart from undefined
behaviors. If this happens, your project will be considered
non functional and will receive a 0.

Instructions:-
- You must edit the sample.c file provided to you such that it
achieves the expected output. (details given in the lower
portion of the document)

- You are provided with a structure in the file called -


info_t which you will work with for printing the output.

- Your task will be divided across two functions:- (1) `main`


and (2) `helper`

- When running the executable, `main` function is provided


first and last names (both lowercase) as command line
arguments into the argument vector.

- In your `main` function, you must:-

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
2
- Create an instance of the info_t structure

- make the structure members `firstname` and `lastname` hold


copies of the command line arguments provided with the
program.

- calculate their respective lengths using `f1` and set the


appropriate values in the structure. `fname_len` will then
contain the length of `firstname` and `lname_len` will
contain the length of `lastname`.
- `f1`: A function pointer to a function that counts the
no. of characters in a string(**NOT** including the
null character). The prototype for this function
pointer exists in the sample.c file.

- Print the contents of the structure from the main function.

- Now move to the `helper` function; making the call to the


function and providing it the info_t structure instance as
the argument.

- In the helper function your task is to:-


- concatenate the strings pointed by `firstname` and
`lastname` using `f2`(f2: A function pointer to a function
that concatenates 2 strings and returns the address of the
concatenated string. Its prototype is listed in the top
section of the sample.c file).
- the member `firstname` must now point to the string
concatenated by `f2`. (Therefore, the string pointed must
read the first name followed by the last name without
space).

- Next, you must capitalize the string pointed by


firstname using ‘f3’(f3: A function pointer to a function
that capitalizes the lowercase characters of a string and
returns the address of the modified string.)

- the value of member `fname_len` should be set to the


length of the concatenated string.

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
3
- print the values of the members in this modified
structure instance. The output should match the expected
output described in the lower section of the document.

- Return to the main function and free any heap allocated


memory you may have allocated.

NOTE!
● you can make `f1`, `f2` and `f3` point to any
functions(library OR user-defined) you deem convenient but
they MUST be executed as function pointers and not called by
function identifiers.

● you may allocate memory on the heap as you see fit using
`malloc()` but before the main function exits, all the heap
allocated memory must be freed(using `free()`) and there
should be no memory leaks. Valgrind is a tool that can help
you check for memory leaks.

Hints
● Capitalized alphabets have their ASCII values 32 less than
the values of their lowercase counterparts.

● In the `helper`function, you may want to reallocate the


memory you assigned to `firstname` since it will then need
to hold a string of a larger length than what had been
originally anticipated.

● Strings end with a null character so you may want to


allocate an extra byte to make room for it.

● Useful functions:-
This document is Private and Confidential.
Seashore Technologies Pte. Ltd.
4
a. - malloc
b. - strlen
c. - strcpy
d. - realloc
e. - strcat
f. - isalpha
g. - islower
h. - toupper
i. - free

Output Expected
Your program must compile without any errors and warnings and it
must not contain any memory leaks.

Given arguments rahul and sharma to the program executable(here:


a.out) in the command line;

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
5
./a.out rahul sharma

the following output is expected:-

Note that the first block in the output is printed by the `main`
function while the second block by the `helper` function in
accordance with the instructions provided in the document.

Submission
● Create a file named Author and list your email ID in the first line of the file followed by a
‘\n’.
● Your final directory must look like this:-

Structures and Pointers

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
6
Turn-in Directory test01

Files to turn in sample.c, Author

● Email the zip file of the directory to talent@tidalwave.tech

Good Luck Guys!

This document is Private and Confidential.


Seashore Technologies Pte. Ltd.
7

You might also like