+ 3
Why this C program is not working?
Hi! I have written a very simple program which will concatinate mine name in dynamically allocated array including null character. Before concatination program will reallocate memoery accordingly. I can't use strcat function because expected output is different. program is concatenating mine name 3 times so output should be "shail\0shail\0shail\0" that means 18 will be size of array. This is not working fine. I don't know what mistake i'm making. Can anyone please help me to make this program work?? https://code.sololearn.com/cm21skTWDujs/#c
15 Answers
+ 4
When you have "shail\0shail\0shail\0" it means you will only see the first substring "shail\0" when you print the string. I'm not clearly understanding what you are doing here, or why.
Maybe you can elaborate more on the actual intention?
+ 4
Shail Murtaza,
Yes I can see that ...
But you still not telling me what is your intention with this code. So I'm having a hard time thinking what to suggest, cause I'm still not getting your actual intention.
+ 3
Ipang
I have implemented mine own print function which will give me output
print_array()
shail0shail0shail0
Where 0 is null character
+ 3
I wrote this for another question before,
https://code.sololearn.com/cTqA6F3B2bfM/?ref=app
The code defines a limit of the elements' length, so it doesn't actually store variable length C-string in, and has overhead due to unused bytes where the C-string is shorter than the limit.
But the benefit is, we can modify an item later on. While new element value length isn't longer than the defined limit, it's fine. Modification of a particular element is rather tricky where variable C-string is used as array elements.
+ 2
Ipang Martin Taylor
Mine true intention is to make array of strings.
Since string itself is an array. You can say that this is 2d array.
Well! I'm not limited to just mine name. I used fixed length of string to make this program simple so anyone can understand easily. I'm using realloc function to allocate memory according to the given input. I doesn't really know what will be the length of given string.
This program is little part of mine actual program.
I wants to make 2d array. But 2d array is inefficient. Wastage of memory. I know it doesn't matter if program is not going to take much memory. But still ....
I doesn't really know why mine program is not working.
+ 2
Lol!
I guess I tried to implement mine own strcpy function.😅
strcpy is doing its job at replacement of strcat_shail.
But now the problem is why mine is not working 😢
+ 2
strcpy() copies string, strcat() appends to string. I guess strcat() looks for '\0' (end of string marker) and uses that address as the first place where new value will be copied into.
But your C-string contains multiple '\0' so it's difficult to deduce where new value should be copied to, or appended to.
+ 2
Ipang
I guess you didn't noticed that I'm not using strcat.
I'm trying to append string after the null character.
There is mark variable which is taking care of it.
I'm also trying to figure out why it is not. Very soon I will find it.
+ 2
Thanks everyone
I really appreciate your help.
Now mine code is working fine
+ 1
Okay bro ...
Good luck 👍
+ 1
In line#52 I changed "*array" to "array" and it worked.
0
انت محترف صراحة