0

In my code below:

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

#define BLOCKSIZE 256;

int main()
{
char text[BLOCKSIZE];

char *new_line;
new_line=strcpy(text,"hello");

int i;
for(i=0;i<sizeof(text)/sizeof(char);i++)
{
printf("%c",*(new_line+i));
}

return 0;

}

I am trying to print the string "hello" on screen using a pointer which points to the address of the char array text. But in my code I get the string hello continued by some garbage values and then leads to core dumped. Can anyone show me the right way? Thanks enter image description here

3
  • 2
    char *ptr = text; while (*ptr) printf("%c",*ptr++); Your code is attempting to print 256 char of "hello" which is only 6 char long. GTG Commented Feb 21, 2015 at 5:07
  • Thanks,What does while(*ptr) do ? What kind of condition is *ptr? Commented Feb 21, 2015 at 5:13
  • *ptr takes the pointer ptr and reads what it points to: some char. The value of that char is tested in while(*ptr) for truth-ness (is it non-zero)? So if the end of the string is not reached (the '\0'), the while loop continues. Commented Feb 21, 2015 at 5:17

3 Answers 3

1

for(i=0;i < sizeof(text)/sizeof(char);i++)

The size of text is 256 bytes as you have allocated 256 bytes to it. sizeof(text)/sizeof(char) would return a value much greater than the size of "hello". That is why the loop is printing garbage values after "hello". You should use i < strlen(text) instead.

Sign up to request clarification or add additional context in comments.

1 Comment

A much better way to write this code is suggested by chux in the comments section under your question.
0

You are printing out all 256 characters of your text array. You only want to iterate up to the length of the string, like this:

for(i = 0; i < strlen(text); i++)
{
   ...
}

Comments

0

As well stated by @tourniquet_grab, code is printing beyond the end of "Hello"

Code copies "Hello" into the ample sized text[], but only the first 6 char (up to and including the terminating null character '\0'). The pointer returned by strcpy() is the address of the first char of text. The remaining 250 char of text has not been initialized. So the following prints "Hello", a '\0' and 250 pieces of junk.

  new_line = strcpy(text,"hello");
  for(i=0;i<sizeof(text)/sizeof(char);i++) {
    printf("%c",*(new_line+i));
  }

More sensible to print the string contents of new_line - only up to, but not including the terminating null character '\0'. This method will change the pointer of new_line.

  new_line = strcpy(text,"hello"); 
  // Continue looping until \0 reached
  while (*new_line)  {
    printf("%c", *new_line++);
  }

Minor points:

sizeof(char) is always 1. Rarely useful to code that. If anything, code sizeof(text)/sizeof(text[0]).

Rather than printf("%c",*new_line++);, could use fputc(*new_line++, stdout) or other 1 char functions like putc()

Comments

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.