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

2
  • 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. Commented Nov 13, 2019 at 9: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. Commented Nov 13, 2019 at 9:36

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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;
}

7 Comments

@TiyaDSoftwareDeveloper What do you mean?
before free() add ptr = NULL; For making sure pointer is not containing any data, which can be junk in future.
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
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.
@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.
|
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;
}

3 Comments

calloc() will increase unnecessary call for initializing ptr with '0'.
True, but it avoids problems with non-null terminated strings.
@kiranBiradar Definitely true yes ! But in that case author missed this '\0'...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.