Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-101
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
orestis.malaspin
rust-101
Commits
e3fecb83
Commit
e3fecb83
authored
1 year ago
by
orestis.malaspin
Browse files
Options
Downloads
Plain Diff
Merge branch 'c_result' into 'main'
Add result in C code See merge request
orestis.malaspin/rust-101!47
parents
e558d069
cd36d081
Branches
Branches containing commit
No related tags found
1 merge request
!47
Add result in C code
Pipeline
#26054
passed
1 year ago
Stage: test
Stage: build
Stage: deploy
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
codes/c_lang/min_list/min_list.c
+22
-23
22 additions, 23 deletions
codes/c_lang/min_list/min_list.c
with
22 additions
and
23 deletions
codes/c_lang/min_list/min_list.c
+
22
−
23
View file @
e3fecb83
...
...
@@ -7,6 +7,8 @@
#define MAX_INT 100
typedef
enum
_result
{
ok
,
err_invalid_size
,
err_is_null
}
result
;
const
char
*
usage_msg
=
"Error wrong number of arguments.
\n
"
"Usage: ./min_list <num1> <num2> ...
\n
"
...
...
@@ -18,13 +20,13 @@ const char *usage_msg =
int
*
list_find_min
(
int32_t
*
tab
,
int
size
);
// Prints all the element in the tab array.
// Returns
-1
if size <= 0
// Returns
-2
if tab is NULL
//
Any other value means that
everything went fine
in
t
list_print
(
int32_t
*
tab
,
int
size
);
// Returns
err_invalid_size
if size <= 0
// Returns
err_is_null
if tab is NULL
//
Returns ok if
everything went fine
resul
t
list_print
(
int32_t
*
tab
,
int
size
);
// Exits if the error code shows invalidity
void
error_handling
(
in
t
error_code
,
int32_t
**
tab
);
void
error_handling
(
resul
t
error_code
,
int32_t
**
tab
);
// Parses a string to an integer.
// Returns a pointer to newly allocated data.
...
...
@@ -57,10 +59,7 @@ int *read_input(int size, char *char_num[]) {
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
==
1
)
{
fprintf
(
stderr
,
"Error wrong number of arguments.
\n
"
"%s"
,
usage_msg
);
fprintf
(
stderr
,
"%s"
,
usage_msg
);
return
EXIT_FAILURE
;
}
...
...
@@ -98,19 +97,19 @@ int min_i32(int32_t lhs, int32_t rhs) {
}
// Checks if size is valid and tab is not NULL
in
t
list_is_valid
(
int32_t
*
tab
,
int
size
)
{
resul
t
list_is_valid
(
int32_t
*
tab
,
int
size
)
{
if
(
size
<=
0
)
{
return
-
1
;
return
err_invalid_size
;
}
if
(
NULL
==
tab
)
{
return
-
2
;
return
err_is_null
;
}
return
0
;
return
ok
;
}
in
t
list_print
(
int32_t
*
tab
,
int
size
)
{
in
t
code
=
list_is_valid
(
tab
,
size
);
if
(
code
<
0
)
{
resul
t
list_print
(
int32_t
*
tab
,
int
size
)
{
resul
t
code
=
list_is_valid
(
tab
,
size
);
if
(
code
!=
ok
)
{
return
code
;
}
...
...
@@ -122,8 +121,8 @@ int list_print(int32_t *tab, int size) {
}
int32_t
*
list_find_min
(
int32_t
*
tab
,
int
size
)
{
in
t
code
=
list_is_valid
(
tab
,
size
);
if
(
code
<
0
)
{
resul
t
code
=
list_is_valid
(
tab
,
size
);
if
(
code
!=
ok
)
{
return
NULL
;
}
...
...
@@ -135,21 +134,21 @@ int32_t *list_find_min(int32_t *tab, int size) {
return
min
;
}
void
error_handling
(
in
t
error_code
,
int32_t
**
tab
)
{
void
error_handling
(
resul
t
error_code
,
int32_t
**
tab
)
{
switch
(
error_code
)
{
case
-
1
:
case
err_invalid_size
:
fprintf
(
stderr
,
"Return value, %d. Size is <= 0.
\n
"
,
error_code
);
free
(
*
tab
);
*
tab
=
NULL
;
exit
(
EXIT_FAILURE
);
break
;
case
-
2
:
case
err_is_null
:
fprintf
(
stderr
,
"Tab is NULL.
\n
"
);
free
(
*
tab
);
*
tab
=
NULL
;
exit
(
EXIT_FAILURE
);
break
;
default
:
case
ok
:
break
;
}
}
...
...
@@ -163,7 +162,7 @@ int32_t *parse_int32(char *arg_to_transform) {
long
arg
=
strtol
(
arg_to_transform
,
&
remaining
,
10
);
// number is parsed in base 10
if
(
*
remaining
!=
'\0'
||
errno
!=
0
)
{
return
NULL
;
// Empty string parsed or an error occurred
return
NULL
;
// Empty string parsed or an error occurred
}
if
(
arg
<
INT_MIN
||
arg
>
INT_MAX
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment