0

So I'm enrolled in a beginner level systems course at my university and my professor mentioned that array sizes cannot be changed because they are set in memory.

Furthermore, that strings in C are just arrays of characters.

So my question is, how can strings of different lengths be copied using strcpy, when the lengths themselves cannot be changed?

(Currently 2:30am and have an exam in 1 day)

Apologies it's a beginner question but any help is very much appreciated!! Thanks :)

JoshPark
  • 19
  • 7
  • 4
    It's late - get some rest before exam. – user2736738 Feb 11 '18 at 07:38
  • If they are in a fixed length , then the strcpy allocates new size in memory and copy both arrays into it. – sharon182 Feb 11 '18 at 07:42
  • 1
    As @coderredoc says - Get some rest. Shore up the bits you understand. Have a good breakfast before the exam and drink some water. It is good for the brain. – Ed Heal Feb 11 '18 at 07:46
  • All a string is - is a character array with a `'\0'` (*nul-terminating character*) that tells you where the end of the string is. (of course a string can simply be sequential bytes in memory, but you get the drift -- ***"if it ain't nul-terminated -- it ain't a string"***) and you can't pass it as a parameter to a function in `string.h` that is expecting a string because the function won't be able to figure out where in the heck the end of the string is and will just keep reading bytes off into memory you don't own looking for one until really bad things happen... – David C. Rankin Feb 11 '18 at 07:47
  • Thanks for all the help guys, really cleared up some misunderstood concepts – JoshPark Feb 11 '18 at 07:55

1 Answers1

5

The simple answer is, the facts don't change even if we copy string using strcpy. Once declared the size of an array won't change. In fact suppose you have a char array

char arr[]="helloworld"; //size of the array will be 11
strcpy(arr,"funny");

What is the size of the array? It's same as when declared. 11. It didn't change from the original one.

What is the length of the string? It's 5 now.

String are null terminated char arrays. That means the array may have 100 elements but the string will be some characters null terminated and it can be anything for example 2 length or 5 length or anything that is possible given that we have space left for storing \0.

Copying a string doesn't mean the array size changes.

Also note one thing very carefully - strings are not just array of characters, it's the NUL terminator at the end of those characters which makes it a string.

char s[4]="hell";

This is legal C code and this is not string. There is no space for \0. You can't use it with standard string library functions. The thing is this will not work with functions like strcpy,strlen etc. Because those implementation depends upon the array of characters to be NUL terminated to work properly.

Beware with the last example shown in mind - writing this is Undefined behavior.

char s[4]="hell";
size_t len = strlen(s);

No you can't do that. strlen expects something and you didn't give it one. So the right way to say things would be- (Here is a nice descriptive quote, I don't recall the source)

In , a string is not an intrinsic type. A C-string is the convention to have a one-dimensional array of characters which is terminated by a null-character, by a '\0'.

1) Don't remember the source of the quote: If anybody does let me know. I kept it in a file.(Not primary source)

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • 2
    Ahh thank you so much, really greatly worded so that it's easily understandable. Will take your advice now and go to bed – JoshPark Feb 11 '18 at 07:54
  • Where does that last quote come from? An attribution might be in order. Or is that your phrasing, placed in block quotes for emphasis? – ad absurdum Feb 11 '18 at 08:13
  • 1
    @DavidBowling.: I kept it in note..but I am afraid I found it from somewhere. (Not mine)..I dont know what to do in this case. I want to Emphasize for sure. I dont know the source. – user2736738 Feb 11 '18 at 08:25
  • You could mention in your answer that you don't know the source. It is a good quote. – ad absurdum Feb 11 '18 at 08:29
  • 1
    @DavidBowling.: You mean like this *From:Unknown source*? Okay done. – user2736738 Feb 11 '18 at 08:30