2

I am new to pointers, please let me know how can i print the entered character.

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

   int main()
   {
      char *ptr;
      ptr = malloc(32 * sizeof(char));
      *ptr = 'h';
      ptr++;
      *ptr = 'e';
      ptr++; 
      *ptr = 'l';
      ptr++;
      *ptr = 'l';
      ptr++;
      *ptr = 'o';
      ptr++;
      *ptr = '\n';

      printf("value entered is %s\n", ptr);

      return 0;
    }

I want to print hello

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    Two issues: When you print `ptr` is no longer pointing to the original location; And `char` strings in C are really called ***null-terminated** byte strings*. This null-terminator is incredibly important for all string handling (including using `printf` with the `%s` format) to know where the string ends. – Some programmer dude Nov 13 '19 at 09:34
  • 2
    Don't increment the original pointer. Have a temporary pointer point at `ptr` right after malloc, then do all arithmetic on the temporary pointer. – Lundin Nov 13 '19 at 09:36

3 Answers3

5

You forgot the null-terminator. Add this:

ptr++;
*ptr = '\0';

Also, the pointer is now pointing to the null-terminator (or previously the newline character). You have to set it back to point to the 'h' again:

ptr -= 6;

And when you're done, you should free the memory:

free(ptr);
Blaze
  • 16,736
  • 2
  • 25
  • 44
4

You should fix your code like this, with a temporary pointer:

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

int main (void)
{
  char* ptr;
  ptr = malloc(32 * sizeof(char));
  if(ptr == NULL)
  {
    puts("Allocation failed");
    return EXIT_FAILURE;
  }

  char* tmp = ptr;

  *tmp = 'h';
  tmp++;
  *tmp = 'e';
  tmp++; 
  *tmp = 'l';
  tmp++;
  *tmp = 'l';
  tmp++;
  *tmp = 'o';
  tmp++;
  *tmp = '\0'; // NOTE: null termination not \n

  printf("value entered is %s\n", ptr);
  free(ptr);    

  return 0;
}

A proper version without messy pointer arithmetic looks like this:

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

int main (void)
{
  char* ptr;
  ptr = malloc(32 * sizeof(char));
  if(ptr == NULL)
  {
    puts("Allocation failed");
    return EXIT_FAILURE;
  }

  strcpy(ptr, "hello");
  printf("value entered is %s\n", ptr);
  free(ptr);

  return 0;
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • @TiyaDSoftwareDeveloper What do you mean? – Lundin Nov 13 '19 at 11:26
  • 1
    before free() add ptr = NULL; For making sure pointer is not containing any data, which can be junk in future. – Shikha-SeniorDeveloper Nov 13 '19 at 11:33
  • 1
    setting a pointer after free? Sorry but that will throw invalid pointer error, as the memory you have u freed and trying to make pointer null. Please correct me if I m wrong – Shikha-SeniorDeveloper Nov 13 '19 at 11:38
  • @TiyaDSoftwareDeveloper Yeah, either I don't understand you or everything you say is wrong. To begin with, the pointer itself is not stored on the heap... – Lundin Nov 13 '19 at 11:43
  • 1
    True, pointer itself is not stored on the heap. So, once you freed, no sense of making it NULL(ptr = NULL), its now beyond your scope. – Shikha-SeniorDeveloper Nov 13 '19 at 11:49
  • 1
    @TiyaDSoftwareDeveloper It's very common practice to set pointers to NULL after freeing them, to allow allocation functions like `free(ptr); ptr = malloc(...)` that free() up old data and can be called at any point, regardless of whether the pointer is pointing to allocated memory or not. This is taking advantage of `free(NULL)` being a no-op. Anyway, this isn't the place for this discussion. – Lundin Nov 13 '19 at 12:03
  • thanks for correcting me. Will remember this in future – Shikha-SeniorDeveloper Nov 14 '19 at 06:41
0

You can, rather using the malloc() function, use the calloc() function, which achieves the same goal of malloc(), but fills the memory with '\0'. This makes it easier to play with non-fixed length strings.
You can find the documentation of this function here.

Here is the code I've made :

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

int main()
{
  char *ptr;
  ptr = calloc(32,sizeof(char));
  *ptr = 'h';
  ptr++;
  *ptr = 'e';
  ptr++; 
  *ptr = 'l';
  ptr++;
  *ptr = 'l';
  ptr++;
  *ptr = 'o';
  ptr++;
  *ptr = '\0';  //It should be null terminated

  ptr -= 5;
  printf("value entered is %s\n", ptr);

  free(ptr);

  return 0;
}
cocool97
  • 1,201
  • 1
  • 10
  • 22