How can I return a char array and print it in main?

Tarasov

I have built a function that gets an integer argument and returns a char array.

For example, for an argument of 13 the function should return "0013"; for an argument of 3 the function should return "0003".

But I don't get an error and I cannot show this value with printf(getInstructionIndex(13));

Here is my code:

/* get Instruction Index how "0001" or "0010" */
char * getInstructionIndex(int index){

    char number1;
    char number2;
    char number3;

    char *str = (char *) malloc(sizeof(char) * 4);

        if(index < 10){

            number1 = '0';
            number2 = '0';
            number3 = '0';

            str[0] = number1;
            str[1] = number2;
            str[2] = number3;
            str[3] = index;

            return str;
        }
        else{
            if(index < 100 && index >= 10){

                number1 = '0';
                number2 = '0';

                str[0] = number1;
                str[1] = number2;
                str[2] = index;

                return str;

            }
            else
            {
                if(index < 1000 && index >= 100){
                    number1 = '0';

                    str[0] = '0';
                    str[1] = index;

                    return str;
                }
                else
                {
                    str[0] = index;

                    return str;
                }
            }
        }

        free(str);

}

Here is my main:

int main(int argc, char *argv[]){

    printf("started\n");


    printf(getInstructionIndex(13)); /* i must see 0013*/

    printf("stopped\n");

    return 0;
}
amdixon

issues

  • string str allocated only 4 chars, not enough for null terminator
  • null terminator not added
  • complex logic
  • memory leak
  • missing format specifier
  • casting malloc return

adjusted code

#include <stdio.h>
#include <stdlib.h>

/* get Instruction Index how "0001" or "0010" */
char * getInstructionIndex(int index){
  /* overflow + underflow handling */
  if(index < 0 || index > 9999)return 0;

  /* allocate enough for the null terminator.. */
  char *str = malloc(sizeof(*str) * 5);
  /* error handling */
  if(!str)
  {
    return 0;
  }

  /* simplify logic, use a nice format specifier */
  sprintf(str, "%04d", index);
  return str;
}

int main(int argc, char *argv[])
{
  char *instruction_index;
  printf("started\n");
  instruction_index = getInstructionIndex(13);
  if(!instruction_index)
  {
    // error handling here..
    return 0;
  }
  /* add format string.. */
  printf("%s\n", instruction_index); /* i must see 0013*/
  /* release the memory */
  free(instruction_index);
  printf("stopped\n");
  return 0;
}

output

$ gcc -g test.c -o test
$ valgrind ./test
==2713== Memcheck, a memory error detector
==2713== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==2713== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==2713== Command: ./test
==2713== 
started
0013
stopped
==2713== 
==2713== HEAP SUMMARY:
==2713==     in use at exit: 0 bytes in 0 blocks
==2713==   total heap usage: 1 allocs, 1 frees, 5 bytes allocated
==2713== 
==2713== All heap blocks were freed -- no leaks are possible
==2713== 
==2713== For counts of detected and suppressed errors, rerun with: -v
==2713== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I return the minimum key in an array?

From Dev

How do I return a pointer to something in the middle of a char array?

From Dev

Why can I increment a char array position inside a function and not in main

From Dev

How can I check if the char array has an empty cell so I can print 0 in it?

From Dev

Why can you return a const char* in main?

From Dev

How to print pointer char array

From Dev

How can I print a multidimensional array in Perl?

From Dev

why can't i return array values to main function?

From Dev

How can I return an array of promises in a then statement

From Dev

How can I print the lowest key of an array?

From Dev

How can I print array in Assembly

From Dev

How can I print an associative array as a matrix

From Dev

how can i filter an array and return matches

From Dev

How can I return to the main menu?

From Dev

How can I return a linked list struct to main in C++?

From Dev

How do I return a pointer to something in the middle of a char array?

From Dev

How can I print this?

From Dev

How do I return an array of int to a main method

From Dev

Can't print element of char array

From Dev

c - how can I print specified count of a char in a line

From Dev

How can I return a 2 dimensions value to main method?

From Dev

How can i print and return a value from a function in python?

From Dev

How can I print the lowest key of an array?

From Dev

how can i filter an array and return matches

From Dev

How should I print out data member of string and int in return type of char?

From Dev

How can i put a char pointer into an array char in a for loop in {C}

From Dev

How can I save an array to UserDefaults, and then retrieve that array and print

From Dev

how I can print an array as a vector form

From Dev

returning a pointer that points to char array. How do i print out the entire contents of the array?

Related Related

  1. 1

    How can I return the minimum key in an array?

  2. 2

    How do I return a pointer to something in the middle of a char array?

  3. 3

    Why can I increment a char array position inside a function and not in main

  4. 4

    How can I check if the char array has an empty cell so I can print 0 in it?

  5. 5

    Why can you return a const char* in main?

  6. 6

    How to print pointer char array

  7. 7

    How can I print a multidimensional array in Perl?

  8. 8

    why can't i return array values to main function?

  9. 9

    How can I return an array of promises in a then statement

  10. 10

    How can I print the lowest key of an array?

  11. 11

    How can I print array in Assembly

  12. 12

    How can I print an associative array as a matrix

  13. 13

    how can i filter an array and return matches

  14. 14

    How can I return to the main menu?

  15. 15

    How can I return a linked list struct to main in C++?

  16. 16

    How do I return a pointer to something in the middle of a char array?

  17. 17

    How can I print this?

  18. 18

    How do I return an array of int to a main method

  19. 19

    Can't print element of char array

  20. 20

    c - how can I print specified count of a char in a line

  21. 21

    How can I return a 2 dimensions value to main method?

  22. 22

    How can i print and return a value from a function in python?

  23. 23

    How can I print the lowest key of an array?

  24. 24

    how can i filter an array and return matches

  25. 25

    How should I print out data member of string and int in return type of char?

  26. 26

    How can i put a char pointer into an array char in a for loop in {C}

  27. 27

    How can I save an array to UserDefaults, and then retrieve that array and print

  28. 28

    how I can print an array as a vector form

  29. 29

    returning a pointer that points to char array. How do i print out the entire contents of the array?

HotTag

Archive