rbaleksandar
October 12th, 2009, 08:45 PM
Hi, guys. :)
Well, I've started making a project of mine written in C. I'm still learning so please not too fancy stuff. :) Here's the deal...But first to mention that I use GCC (it might have something to do with my problem...perhaps ;)).
I want to define 2 string (one dim. arrays in C). The first one is a non-fixed-size array that is:
char array1[] = "hello"
Then I define the second array with:
char array2[strlen(array1)+1]
It takes the size of the array. I think it's possible but I'm not so sure since the array where I take the size of the second one is actually non-fixed-size and yet I define a start-value of array1, which is "hello" (length = 6 with the NULL at the end). So when I initialize the first array, it's been given a temporary size of 5 (NULL isn't counted when you call the array with strlen())... I think it works like that at least. :D
I presume that my thoughts are correct and continue my story. ;) Now the problem is that when I call strlen() for both arrays I get cr*p (apologize for the expression but I feel it that way):
printf("%d", strlen(array1)); //returns 5, which is correct
printf("%d", strlen(array2)); //returns 3
You see where the problem is...The evil 3. Ok, I suppose the code sucks and it's wrong and yet - where the hell does this 3 come from?! Even if I define my array2 with
char array2[strlen(array1) + 1000000000] it returns 3. :confused:
I want that in my program the first array is predefined and the second to be "dynamic" and fill it with as many characters as the first one.
I tried to make array1 with a fixed size of 6 (hello is 5 characters + 1 characters for the NULL) and still the damn length of array2 is 3. :(
Would appreciate if someone can explain this mystery (at least for that's what it is :)).
Thanks in advance!
Well, I've started making a project of mine written in C. I'm still learning so please not too fancy stuff. :) Here's the deal...But first to mention that I use GCC (it might have something to do with my problem...perhaps ;)).
I want to define 2 string (one dim. arrays in C). The first one is a non-fixed-size array that is:
char array1[] = "hello"
Then I define the second array with:
char array2[strlen(array1)+1]
It takes the size of the array. I think it's possible but I'm not so sure since the array where I take the size of the second one is actually non-fixed-size and yet I define a start-value of array1, which is "hello" (length = 6 with the NULL at the end). So when I initialize the first array, it's been given a temporary size of 5 (NULL isn't counted when you call the array with strlen())... I think it works like that at least. :D
I presume that my thoughts are correct and continue my story. ;) Now the problem is that when I call strlen() for both arrays I get cr*p (apologize for the expression but I feel it that way):
printf("%d", strlen(array1)); //returns 5, which is correct
printf("%d", strlen(array2)); //returns 3
You see where the problem is...The evil 3. Ok, I suppose the code sucks and it's wrong and yet - where the hell does this 3 come from?! Even if I define my array2 with
char array2[strlen(array1) + 1000000000] it returns 3. :confused:
I want that in my program the first array is predefined and the second to be "dynamic" and fill it with as many characters as the first one.
I tried to make array1 with a fixed size of 6 (hello is 5 characters + 1 characters for the NULL) and still the damn length of array2 is 3. :(
Would appreciate if someone can explain this mystery (at least for that's what it is :)).
Thanks in advance!