0
#include <stdio.h>    

int main()  
{  
  char s[] = "churchgate: no church no gate";  
  char t[25];  
  char *ss, *tt;  
  ss = s;  
  while (*ss != '\0')  
    *tt++ = *ss++;  
  printf("%s\n", t);  
  return 0;  
}  

what is the problem with this code? when I try to run it. Its showing up some garbage values.

4
  • 2
    Writing through an uninitialized pointer is undefined behavior. Commented Nov 12, 2012 at 20:11
  • Add static_assert(sizeof(s) <= 25, "Bad bad bad."); Commented Nov 12, 2012 at 20:15
  • 1
    @Lundin, I'd suggest char t[sizeof(s)]; instead. Especially, taking into account it's c-tagged. Commented Nov 12, 2012 at 20:16
  • @MichaelKrelin-hacker That's fine too, of course. And static_assert is part of the C language, since C11. Commented Nov 12, 2012 at 20:25

5 Answers 5

3

You never point tt to anything. You need to point it to t:

tt=t; 
Sign up to request clarification or add additional context in comments.

Comments

3
  1. You forgot to inialize tt to t
  2. Your array is too small.
  3. You forgot to null terminate your array.

2 Comments

Who cares about 2, given 1? :)
@MichaelKrelin-hacker yes, I'm trying to be exhaustive;)
1

Tho it can be fun to experiment with arbitrary locations in memory, if you want a defined behaviour the target of access has to be defined.

tt has to be pointed towards some defined area in memory space before you do operations on it.

*tt++ = *ss++;

s is 30 bytes. t, if that is the one you want to use for tt is 25.

2 Comments

@MichaelKrelin-hacker; you are very correct, I was a bit to dyslectic in counting chars there. Thanks :)
That's because instead of counting I've pasted it into console echo -n ....|wc -c (and added one for terminator) ;-)
0

Several problems:

1) "tt" was never initialized, and

2) "s[]" might be READ ONLY (depending on compiler/platform)!!!!!

Suggestion:

#include <stdio.h>

#define MAX_STRING 80

int 
main()  
{  
  char s[MAX_STRING] = "churchgate: no church no gate";
  char t[MAX_STRING];  
  char *ss = s, *tt = t;
  while (*ss)  
    *tt++ = *ss++;
  *tt = '\0'; 
  printf("%s\n", t);  
  return 0;  
}  

Comments

0

Several possibilities, e. g.:

#include <stdio.h>    

int main()  
{  
  char s[] = "churchgate: no church no gate";  
  char t[25];  
  char *ss, *tt;  
  for (ss=s, tt=t; *ss != '\0' && tt-t < sizeof(t)-1; ss++, tt++)
    *tt = *ss;
  }
  *tt = '\0';

  // or just use strncpy.
  // strncpy doesn't copy the \0 if the size is reached,
  // so account for that.
  strncpy(t, s, sizeof(t)-1);
  t[sizeof(t)-1] = '\0';

  printf("%s\n", t);
  return 0;  
}

You know your major problems from the other answers:

  1. tt uninitialized
  2. no bounds checking for t
  3. no 0-termination after copying

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.