-1

After reading some articles I stayed a few questions:

  1. When I have an uninitialized pointer I can not put into it because there is no memory allocation place! When I have a pointer that points to a variable of type char and I want to put into it with %s, the program lets me put up two characters (2 bytes) and beyond that prints me an error. My question is why it is can be put into two letters when char can hold only one byte? Is the program should not fall even two letters?

  2. It can't put strings into an array like this: arr = "aaa" Because arr are actually address and not variable that can hold values ​​of char?

  3. I read a little about undefined behavior, is it possible to understand that there is a discount that will not use a char pointer to strings and therefore the printing of the string that points to char behaves in an unexpected way?

Hodaya Shalom
  • 4,327
  • 12
  • 57
  • 111
  • The compiler generates code which assumes you follow the spec. If you don't follow the spec then anything can happen. The compiler does not always have to diagnose it when you don't follow the spec. – M.M Jul 23 '14 at 05:33
  • 2
    That's too broad for one question, split them up. And I think some of them have duplicates in SO already, make sure you searched for them first. – Yu Hao Jul 23 '14 at 05:34
  • Take a look at the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Jul 23 '14 at 05:37
  • 1
    check i Already answered for some doubt [here](http://stackoverflow.com/questions/22703720/i-feel-confusion-about-bus-error-in-string-c/22703925#22703925) – Jayesh Bhoi Jul 23 '14 at 05:42
  • @Jayesh! Thanks for an excellent answer! I remove this section from the questions. – Hodaya Shalom Jul 23 '14 at 05:55
  • Possible duplicate of: http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c – Igor Pejic Jul 23 '14 at 05:55
  • @KeithThompson. It's not to broad ,apart from the first question, the answers are yes and no! – Hodaya Shalom Jul 23 '14 at 05:57
  • 1
    Why tell me it's not too broad? I'm not the one who said it was. (Though now that you mention it, it probably is.) – Keith Thompson Jul 23 '14 at 06:56

1 Answers1

1

1.This might be related to alignment. If there is a multibyte type following a char variable, there may be a small number of unused bytes in between but you can not depend on this as it is clearly undefined.

I would suggest using valgrind to check for memory problems rather than depending on observation.

2.The difference here is that

char *s = "Hello world"; will place Hello world in the read-only parts of the memory and making s a pointer to that, making any writing operation on this memory illegal. While doing:

char s[] = "Hello world"; puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. Thus making

s[0] = 'J'; legal.

3.There are no string pointers, a char pointer is also used for strings. When you want to point to strings a char pointer is used. The problem is that you are trying to store two letters on the place of one.

Igor Pejic
  • 3,658
  • 1
  • 14
  • 32