+ 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

19th Dec 2021, 1:48 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
12 Respostas
+ 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?
19th Dec 2021, 3:08 PM
Ipang
+ 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.
19th Dec 2021, 3:37 PM
Ipang
+ 3
Ipang I have implemented mine own print function which will give me output print_array() shail0shail0shail0 Where 0 is null character
19th Dec 2021, 3:22 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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.
19th Dec 2021, 5:24 PM
Ipang
+ 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.
19th Dec 2021, 4:58 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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 😢
19th Dec 2021, 5:30 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 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.
19th Dec 2021, 5:43 PM
Ipang
+ 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.
19th Dec 2021, 5:46 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 2
Thanks everyone I really appreciate your help. Now mine code is working fine
20th Dec 2021, 8:09 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
Okay bro ... Good luck 👍
19th Dec 2021, 5:48 PM
Ipang
+ 1
In line#52 I changed "*array" to "array" and it worked.
19th Dec 2021, 8:53 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
انت محترف صراحة
19th Dec 2021, 8:04 PM
ABDELHADI w