So since I'm just new with learning C (this is the second day now), I'd be very happy if someone could review my code:
what I tried to do is:
- create a structure containing a string- and a length-property
- I want to create a function, that checks for the length of the string and sets the
length
property to the result - check, whether the result is correct with comparing to
strlen
of the original string - print the length of the string
This all works so far. Do you have any hints, what I could do better (or is ugly), what is wrong or where are any risks in my code?
this is my "helloworld.h" header file:
#include <stdio.h>
#include <stdlib.h>
struct SomeStringStruct
{
char* actual_string;
int length;
};
void calculate_length(struct SomeStringStruct*);
int get_string_length(char*);
and this is my "helloworld.c" source file:
#include <stdio.h>
#include <stdlib.h>
#include "helloworld.h"
int main(int argc, char* argv[])
{
char string[] = "This is a test";
char* ptrString = string;
printf("strlen ptrString: %lu\n", strlen(ptrString));
printf("get_string_length ptrString: %lu\n", get_string_length(ptrString));
return 0;
}
void calculate_length(struct SomeStringStruct* ptrAnyStringStruct)
{
ptrAnyStringStruct->length = 0;
while(ptrAnyStringStruct->actual_string[ptrAnyStringStruct->length] != '\0')
ptrAnyStringStruct->length++;
}
int get_string_length(char* string)
{
struct SomeStringStruct myString;
myString.actual_string = string;
calculate_length(&myString);
return myString.length;
};