I'm having a little problem with a (pretty big) c program and figured out that the problem does not come from my program itself but the way I create my array, I think.
My problem is the following, I need to create an array who's identical to the one containing environment variables (extern char **environ) and then add another value at the end.
Here is a code that I've done to check if the environ is well copied or not:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int ft_length(char **arr)
{
int i = 0;
while (arr[i])
i++;
return (i);
}
int main(void)
{
extern char **environ;
char **env;
int err;
int x;
err = 0;
x = 0;
env = (char**)malloc(sizeof(char*) * ft_length(environ) + 2);
while (environ[x])
{
// printf("%s\n", environ[x]);
env[x] = (char*)malloc(sizeof(char) * strlen(environ[x]) + 1);
bzero(env[x], strlen(environ[x]));
strncpy(env[x], environ[x], strlen(environ[x]));
// printf("%s\n", env[x]);
x++;
}
env[x] = (char*)malloc(sizeof(char) * 4);
env[x] = "ccc\0";
env[++x] = NULL;
x = 0;
while (environ[x])
{
if (strcmp(environ[x], env[x]) != 0)
{
err++;
printf("error on env[%d]\n", x);
printf("environ[%d] : |%s|\n", x, environ[x]);
printf("env[%d] : |%s|\n", x, env[x]);
printf("----------------\n");
}
x++;
}
while (env[x])
{
if (strcmp(env[x], "ccc") != 0)
{
err++;
printf("env[%d] contain |%s| instead of |ccc|\n", x, env[x]);
}
x++;
}
printf("done with %d error(s)\n", err);
return (1);
}
When I run this code on Mac OSX, env[0] is empty at the end, and that happen when I set the last array value to NULL (env[++x] = NULL). But, well, I need this one to be NULL if I want to print my array without segfault.
So, first I wanted to know if my code is wrong somewhere?
I tried to run this code on my Linux computer (Ubuntu 16.04) too and it seems there is no problem.
malloc(sizeof(char*) * ft_length(environ) + 2)
If you need two additional elements in the array, that'd be
malloc(sizeof(char*) * (ft_length(environ) + 2));
Or use calloc:
calloc (ft_length(environ) + 2, sizeof(char*));
In addition
env[x] = (char*)malloc(sizeof(char) * 4);
env[x] = "ccc\0";
is another pitfall everybody seems to fall in. You assign a pointer value to env[x], then immediately regret and assign another pointer value to it, forgetting the first one, creating a memory leak in process, and making your env impossible to free as the new string is not pointing to a dynamically allocated memory. This is particularly baffling as you seem to correctly (even if with much redundancy) copy a string a few lines earlier.
env[x] = malloc(4); // sizeof(char) is always one; casting not needed
// also see below
strcpy (env[x], "ccc"); // of course no `\0` is needed
Since you have code that dynamically allocates a copy of a string in two places, you may want to make this code into a function, or perhaps use an existing (albeit non-standard) function strdup. This is particularly important because there's no good way to copy a string literal otherwise.
env[x] = malloc(4);
strcpy (env[x], "ccc");
Assumes the length. What if the literal will change?
env[x] = malloc(strlen("ccc")+1);
strcpy (env[x], "ccc");
What if someone changes one literal and forgets the other?
const char ccc[] = "ccc";
env[x] = malloc(sizeof(ccc));
strcpy (env[x], ccc);
This works but you have an extra line that declares a variable. This doesn't sound like much but why?
env[x] = strdup("ccc");
Looks better, maintains better.
If you write your own strdup don't do this
out = (char*)malloc(sizeof(char) * strlen(in) + 1);
bzero(out, strlen(in));
strncpy(out, in, strlen(in));
but rather
out = malloc(strlen(in) + 1);
strcpy(out, in);
bzero is redundant because it will zero out exactly the bytes that strcpy will overwrite with non-zero values at the next line. The strncpy + strlen combo is plain wrong as it doesn't null-terminate the destination string. strncpy(out, in, strlen(in)+1) would be correct but redundant, as it does exactly the same thing as plain strcpy(out, in).
Related
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 1 year ago.
I'm trying to 'deep copy' a string so that I can perform operations on one copy, while retaining the original copy. This is the base example that I've got and for some reason the strncpy call causes a segfault. Please help
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main() {
char* stringA = "someVeryinTeresTingString";
char* stringB = malloc(sizeof(char) * strlen(stringA));
printf("A: %s, B: %s\n", stringA, stringB);
for (int i = 0; i < strlen(stringA); i++) {
stringB[i] = tolower(stringA[i]);
}
printf("A: %s, B: %s\n", stringA, stringB);
strncpy(stringA, stringB, strlen(stringA) - 1);
printf("A: %s, B: %s\n", stringA, stringB);
}
Easiest fix is to make a local copy of that string literal:
char stringA[] = "someVeryinTeresTingString";
Everything else works just the same.
Note that in the original code you have a pointer to immutable memory, while in this version you have a local (stack) array that is initialized with a copy of that string.
Another thing to note is if you're copying and manipulating C strings, do things like this:
char* stringB = strdup(stringA);
for (int i = 0; i < strlen(stringB); ++i) {
stringB[i] = tolower(stringB[i]);
}
Or even more efficiently by avoiding all these expensive strlen() calls:
char* stringB = strdup(stringA);
for (char* p = stringB; *p; ++p) {
*p = tolower(*p);
}
This line:
char* stringB = malloc(sizeof(char) * strlen(stringA));
shuld be like this:
char* stringB = malloc(sizeof(char) * (strlen(stringA) + 1));
then you will able to copy the \0 in the end of stringA
also, you want to copy to literal string - that is segmentation fault
char *strncpy(char *dest, const char *src, size_t n)
I'll try to comment and correct in your own code the mistakes I've seen:
(I will not correct things that can be eliminated or better done in another way, but are correct or not harmful, so you'll see only what must be corrected because of programming errors, and not questions about style or programming uses)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int main() {
char* stringA = "someVeryinTeresTingString";
/* you need to consider the space for the final null character in the malloc() call */
char* stringB = malloc(sizeof(char) * (strlen(stringA) + 1));
/* you don't need to use sizeof(char) as it is always equal to one.
* Multiplying by one is not necessary, but you'll probably know.
* char is warranteed by C standard that its sizeof is one. */
/* you need to copy the string *before* printing, or you will print an
* uninitialized string. Or at least initialize stringB to zeros, so you can
* use it with printf like functions (I do initialize the first char position to
* zero to make it appear as a length zero "" string)
* You will incurr in undefined behaviour if you don't do this. */
stringB[0] = '\0';
printf("A: %s, B: %s\n", stringA, stringB);
/* you need to copy the strings, so you can do it better if you test when
* stringA[i] == '\0', so you don't calculate the length of a string that is
* not going to change at every loop iteration. I will not change your
* code, because this is not an error. But strlen() searches from the
* beginning of the string for the '\0' char, character by character,
* and this test is done at every loop iteration. With the expression
* stringA[i] == 0 you do only a test per loop iteration to see if
* the char at position i in stringA is the null character. */
int i;
for (i = 0; i < strlen(stringA); i++) {
stringB[i] = tolower(stringA[i]);
}
/* you have not copied the final '\0', so I do it now. I need to move the
* declaration of i outside of the loop to be able to use it's value. */
stringB[i] = 0; /* you can use 0 or '\0' interchangeably */
printf("A: %s, B: %s\n", stringA, stringB);
/* nope. you need to copy the strings with a normal strcpy() as you know that
* both are the same length (better, you know that the space in stringB
* is the same as the length of stringA plus one). If you do this, you will not copy the last '\0' char, so wee need to append it.
* well, I don't know if that is what you want, so I don't actually touch anything here. */
strncpy(stringA, stringB, strlen(stringA) - 1);
/* stringB should be one char shorter than stringA */
printf("A: %s, B: %s\n", stringA, stringB);
}
by the way, you have been recommended to use strdup(3). This is a good idea, you don't need to be thinking on final nulls in this case, because strdup() takes care of it. Just remember that strdup(3) is not included in many C standard revisions, so you can get in trouble if you
move your program to a place lacking it (that should be very strange, anyway)
I'm having some issue with my stack implementation, my push function manipulate the value i send into the function and changes it. I have tried diffrent ways of constructing this but they either don't work or give me corrupted output.
The base idea is the one below here, note: my pop function only walks down one position and doesn't free the memory at the specific position. I can not use strcpy since im working with threads.
Does strdup change the value that it copies, i cant find any information saying that is the case, my understanding is that you are suppose to be able to use the value after it has ben duped.
And how is the correct way to use strdup on a already allocated memory space, i assume that i can't just free it and then use it again.
void stack_push(Stack *s, char *value)
{
if (s->size == s->capacity) {
realloc_stack(s);
}
if(s->data[s->size] == NULL){
// The current position does not contain any data.
s->data[s->size] = strdup(value);
}
else{
free(s->data[s->size]);
s->data[s->size] = strndup(value, strlen(value) + 1);
}
s->size += 1;
}
Edit s->data = char **data
strdup is basically this (no error checking for brevity):
char *strdup(const char *stringtoduplicate)
{
char *newstring = malloc(strlen(stringtoduplicate) + 1);
strcpy(newstring, stringtoduplicate);
return newstring;
}
You use it like this:
char Foo[] = "Bar";
char *newBar = strdup(Foo);
Foo[0] = 'F';
printf("%s %s\n", Foo, newBar); // prints: Far Bar
...
free(newBar); // once you're done with newBar, free it
Now you should be able to answer your own question.
strdup does not in any way modify its argument. If you look at the prototype for strdup you will see that its parameter is declared const, which means that it is not modified.
strdup can be implemented as:
char* strdup(const char* s) {
char* n = malloc(strlen(s) + 1);
if (n) strcpy(n, s);
return n;
}
There is no magic.
You can use strcpy with threads, by the way. But strdup works fine.
I'm currently trying to make a program in c which will return a pointer to an array of 2 strings. The first is the characters of the string s that are in the odd position and the second are the characters in the even position. I'm not experienced in C so I need a bit of help with this program. I've been trying to code using what I know from python and java but it doesn't seem to follow the same principles with pointers. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **parity_strings(const char *s){
char dest[malloc((char)sizeof(s)/2 + 1)][malloc((char)sizeof(s)/2 + 1)]; //trying to allocate memory to an array of size 2 which will hold 2 strings.
int i;
for(i = 0; i < sizeof(s); i+= 2){ //iterating through odd strings
s[0] += dest[i];
}
for(i= 2; i< sizeof(s); i += 2){ //iterating through even strings (I suppose i could have just appended using 1 for loop but oh well
s[1] += dest[i];
}
return dest;
}
int main(int argc, char **argv) {
char **r = parity_strings(argv[1]);
printf("%s %s %s\n", r[0], r[1], argv[1]);
return 0;
}
memory allocation is just a pain too...I have no clue if it's doing what I intend on it doing. I'm trying to allocate the size of the string in bytes + 1 byte into each index of the array Dest.
any ideas on how to fix this? Thanks.
This line will not do anything good:
char dest[malloc((char)sizeof(s)/2 + 1)][malloc((char)sizeof(s)/2 + 1)];
malloc returns a pointer to the newly allocated memory. In your line above, the square brackets in dest[][] need unsigned integers. Pointers can be casted to integers, but that isn’t what you want there at all. It might compile, but it probably won’t run, and certainly won’t do what you want.
Also, sizeof(s) returns the size of the pointer to s, not the length of the string. Strings in C are really just null-terminated arrays of chars, and arrays are passed to functions with a pointer, not their entire contents. To get the length of a string, use strlen(s) instead.
You could do something like this:
char *destodd = malloc((strlen(s)/2 + 2));
char *desteven = malloc((strlen(s)/2 + 2));
char **dest = malloc(sizeof(char *) * 2);
dest[0] = desteven;
dest[1] = destodd;
I changed your + 1 above to +2. A string of length 3 needs 3 characters in destodd: one for character 1, one for character 3, and one for the NUL terminator.
It’s tricky to malloc a multi-dimensional array in C. A one-dimensional array, on the other hand, is easy. Just treat destodd and desteven like they’re arrays, even though they’re really pointers:
for (i = 0; i < strlen(s); i += 2){
desteven[i] = 'a'; // Fix this
destodd[i] = 'b';
}
The code in your for loops didn’t look like it would work. It looks like you may have been trying to use += to concatenate strings, but it only does addition of numbers. I couldn’t quickly figure out what you should set in the for loop, so 'a' and 'b' are just placeholders.
You have a few issues. As your compiler should tell you, char dest[malloc()] requires a pointer-to-unsigned cast, which is legal but is not what you want. More importantly, returning a pointer to an array allocated on the stack results in undefined behavior if you dereference the pointer, because the compiler may have already deallocated the memory. I'm not exactly sure what the intended output of the function is, but in terms of filling two char arrays, in my opinion the easiest way to do it is this:
char **parity_strings(char* buf) //Please avoid single letter variable names for anything but loop control
{
size_t buflen = strlen(buf);
if (NULL == char** dest = malloc(2 * sizeof(*dest)))
;//handle memory allocation error
if (NULL == dest[0] = malloc(buflen * sizeof(*buf)))
;//handle memory allocation error
if (NULL == dest[1] = malloc(buflen * sizeof(*buf)))
;//handle memory allocation error
//Note that you would do the above two lines in a loop for a variable sized multidimensional array
strncpy(dest[0], buf, 500);
strncpy(dest[1], buf, 500); //If you need strings larger than 500 change as necessary, mostly only needed if you are taking input from someone else but it's good practice to use strncpy over strcpy)
return dest;
}
So I'm working through "Sams Teach Yourself C Programming in One Hour a Day, Seventh Edition" Lesson 10 Exercise 7 which asks to "Write a function that accepts two strings. Use the malloc() function to allocate enough memory to hold the two strings after they have been concatenated (linked). Return a pointer to this new string."
I am sure there are much more elegant ways to go about this than what I have attempted below. I am mostly interested in why my solution doesn't work. I have only been learning C for a few months and have no significant programming background. Please let me know why this crashes on compilation. I am using Code Blocks on Win 7 with GNU GCC Compiler if that makes a difference. Thank you :)
#include <stdio.h>
#include <stdlib.h>
char * concatenated(char array1[], char array2[]);
int ctrtotal;
int main(void)
{
char *comboString;
char *array1 = "You\'re the man ";
char *array2 = "Now Dog!";
comboString = (char *)malloc(ctrtotal * sizeof(char));
concatenated(array1, array2);
if (comboString == NULL)
{
puts("Memory error");
exit(1);
}
puts(comboString);
free(comboString);
return 0;
}
char * concatenated(char array1[], char array2[])
{
char *array3;
int ctr;
int ctr2;
for (ctr = 0; array1[ctr] != '\0'; ctr++)
array3[ctr] = array1[ctr];
ctr2 = ctr;
for (ctr = 0; array2[ctr] != '\0'; ctr++)
{
array3[ctr2 + ctr] = array2[ctr];
}
array3[ctr2 + ctr + 1] = '\0';
ctrtotal = (ctr2 + ctr + 2);
return array3;
}
Thank you for the help. After reviewing everyone's feedback on my errors I revised the code to the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * concatenated(char array1[], char array2[]);
int main(void)
{
char *array1 = "Testing Testing One Two ";
char *array2 = "Three. Finally, not crashing the mem o ry.";
char *comboString = malloc( (strlen(array1)+strlen(array2) + 1)*sizeof(char));
comboString = concatenated(array1, array2);
if (comboString == NULL)
{
puts("Memory error");
exit(1);
}
puts(comboString);
free(comboString);
return 0;
}
char * concatenated(char array1[], char array2[])
{
char *array3;
array3 = malloc( (strlen(array1)+strlen(array2) + 1)*sizeof(char) );
strcat(array3, array1);
strcat(array3, array2);
return array3;
}
If anyone sees any redundancies/unnecessary remaining code the could/should be deleted, please let me know. I recognize the benefit of being as concise as possible.
Your code has a bunch of issues:
int ctrtotal is never initialized, so you are mallocing 0 bytes
concatenated() is copying characters to an uninitialized array3. This pointer should point to a mallocd buffer.
If concatenated is allocating the memory, then main doesn't need to. Instead it should use the result of concatenated.
I don't want to give you the full code, and let you to miss out on this learning opportunity. So concatenated should look like this, in psuedo-code:
count = length_of(string1) + length_of(string2) + 1
buffer = malloc(count)
copy string1 to buffer
copy string2 to buffer, after string1
set the last byte of buffer to '\0' (NUL)
return buffer
In C, strings are represented as a NUL-terminated array of characters. That's why we allocate one additional byte, and terminate it with \0.
As a side-note, when dealing with strings, it is far easier to work with pointers, instead of treating them as arrays and accessing them via indices.
There's a lot of code here that just doesn't make any sense. I suggest that you first write this program on paper. Then, "execute" the program in your head, stepping through every line. If you get to something you don't understand, then you need to either fix your understanding, or your incorrect code. Don't try to write code that looks like some other bit of code.
There's also a library function called strcat which will make this task even easier. See if you can figure out how to use it here.
Spoiler --> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *concatenate2(const char* s1, const char* s2);
int main(void)
{
char *comboString;
char *array1 = "You're the man ";
char *array2 = "Now Dog!";
comboString = concatenate2(array1, array2);
if (comboString == NULL)
{
puts("Memory error");
exit(1);
}
puts(comboString);
free(comboString);
return 0;
}
char *concatenate2(const char* s1, const char* s2)
{
char *result;
result = malloc(strlen(s1) + strlen(s2) + 1);
*result = '\0';
strcat(result, s1);
strcat(result, s2);
return result;
}
You forgot to allocate memory for third, concatenated, array of chars (in function)
You should do something like this:
char *array3;
array3 = (char *)malloc( (strlen(array1)+strlen(array2) + 1)*sizeof(char) ); // +1 for '\0' character.
and then write chars from first and second array into third.
Perhaps a stroll through the question code is best.
#include <stdio.h>
#include <stdlib.h>
char * concatenated(char array1[], char array2[]);
int ctrtotal;
Notice that the above line declares ctrtotal to be an integer, but does not specify the value of the integer.
int main(void)
{
char *comboString;
char *array1 = "You\'re the man ";
char *array2 = "Now Dog!";
comboString = (char *)malloc(ctrtotal * sizeof(char));
Notice that the above line allocates memory and sets 'comboString' to point at that memory. However, how much memory is being allocated?
(ctrtotal[???] * sizeof(char)[1])
What is the value of (??? * 1) ? This is a problem.
concatenated(array1, array2);
The intent of the line above is that array1["You\'re the man "] and array2["Now Dog!"] will be joined to form a new string["You\'re the man Now Dog!"], which will be placed in allocated memory and returned to the caller.
Unfortunately, the returned memory containing the string is not captured here. For example, perhaps the above line should be:
comboString = concatenated(array1, array2);
While this make sense, for this line, it begs a question of the purpose of the lines:
comboString = (char *)malloc(ctrtotal * sizeof(char));
as well as the global variable:
int ctrtotal;
and the later reference:
ctrtotal = (ctr2 + ctr + 2);
Perhaps all of these 3 lines should be deleted?
if (comboString == NULL)
{
puts("Memory error");
exit(1);
}
puts(comboString);
free(comboString);
return 0;
}
char * concatenated(char array1[], char array2[])
{
char *array3;
Notice that '*array3' is now a defined pointer, but it is not pointing anywhere specific.
int ctr;
int ctr2;
The purpose of 'concatenated()' is to join array1 and array1 into allocated array3. Unfortunately, no memory is allocated to array3.
Below, the memory where array3 is pointing will be modified. Since array3 is not pointing anywhere specific, this is not safe.
Prior to modifying memory where array 3 is pointing, it is important to point array3 at memory where it is safe to modify bytes. I suggest that the following code be inserted here:
array3 = malloc(strlen(array1) + strlen(array2) + 1);
Now, array3 points to allocated memory, large enough to hold both strings plus the string termination character '\0'.
for (ctr = 0; array1[ctr] != '\0'; ctr++)
array3[ctr] = array1[ctr];
ctr2 = ctr;
for (ctr = 0; array2[ctr] != '\0'; ctr++)
{
array3[ctr2 + ctr] = array2[ctr];
}
array3[ctr2 + ctr + 1] = '\0';
ctrtotal = (ctr2 + ctr + 2);
return array3;
}
I am responding to your revised code. There are a few bugs in it.
...
char *array2 = "Three. Finally, not crashing the mem o ry.";
char *comboString = malloc( (strlen(array1)+strlen(array2) + 1)*sizeof(char));
comboString = concatenated(array1, array2);
...
The malloc is unnecessary here and actually a bug in your code. You are allocating a block of memory, but you then replace the value of the pointer comboString with the pointer from the call to concatenated. You lose the pointer to the block of memory allocated in main and thus never are able to free it. Although this will not be a problem in the code you have right now since main returns soon after, it could cause a memory leak in an application that ran for a longer time.
strcat(array3, array1);
This is also a bug. strcat is going to walk through array3 to find '\0' and then once it is found copy in array1 from that index on, replacing the '\0'. This works fine here since the memory block that was allocated for array3 is going to be zeroed out** as no block has yet been freed by your program. However, in a longer running program you can end up with a block that does not start with a '\0'. You might end up corrupting your heap, getting a segfault, etc.
To fix this, you should use strcpy instead, array3[0] = '\0', or *array3 = '\0'
** When the operating system starts your program it will initialize the memory segment it reserves for it with zeroes (this actually isn't a necessity but will be true on almost any operating system). As your program allocates and frees memory, you will eventually wind up with values that are not zero. Note that the same bug can occur with uninitialized local variables such as:
int i;
for (; i < 10; i++);
This loop will run 10 times whenever the space on the runtime stack where i is stored is already 0.
Overall, the takeaway is to be very careful with arrays and dynamic memory allocation in C. C offers you none of the protections that modern languages do. You are responsible for making sure you stay within the bounds of your array, initialize your variables, and properly allocate and free your memory. Neglecting these things will lead to obscure bugs that will take you hours to find, and most of the times these bugs will not appear right away.
I'm having a segmentation fault when I want to save a string in a dynamic array.
I have a program that does this:
User insert char "s"
The program enters a loop and save strings in an array (name: cod).
When user inserts char "t", it stops
After that I save that array in the first position of a new dynamic array (name: vec).
Then if user insert char "s" again
The program enters a loop and save strings in an array.
When user inserts char "t", it stops
After that I save that array in the second position of a new dynamic array.
and so one.
This is my code:
int main(){
char Cod[30][11];
char tmp[11];
char ***vec;
int i = 0;
strcpy (tmp, "p");
vec = (char *** ) malloc (sizeof ( char *) );
vec[0] = (char ** ) malloc (sizeof ( char *) * 30);
do {
scanf("%s", tmp);
while( (strcmp (tmp, "p")) != 0){
strcpy ( Cod[i] , tmp );
scanf("%s", tmp);
i++;
}
vec = (char ***) realloc (vec, sizeof ( char *) * (i + 1));
vec[i + 1] = (char ** ) realloc (vec[i + 1], sizeof ( char *) * (30));
vec[i-1] = (char **) Cod;
scanf("%s", tmp);
}
while((strcmp (tmp, "s")) == 0);
printf("%s", vec[0][0]);
return 0;
}
This is the part of the code that work's:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char Cod[30][11];
char tmp[11];
int i = 0;
strcpy (tmp, "p");
do {
scanf("%s", tmp);
while( (strcmp (tmp, "p")) != 0){
strcpy ( Cod[i] , tmp );
scanf("%s", tmp);
i++;
}
scanf("%s", tmp);
}
while((strcmp (tmp, "s")) == 0);
printf("%s", Cod[0]);
return 0;
}
I started to fix this code but soon realized there is so much wrong in it, I don't know where to start. So instead, this turned out to be a code review answer instead, I apologize up front if it is detailed and picky.
There's a rule of thumb in C saying that if you need more than two levels of pointer indirection, your code is obfuscated and should be rewritten (reference MISRA-C:2004 17.5).
It doesn't make sense whatsoever to use dynamic memory allocation in this case, because you already know at program start-up that no string will be larger than 11 characters and there will not be more than 30 strings. If this condition is not true, you need to write a safer input method, preferably with fgets() which is safe against buffer overflows. Make sure that the input doesn't go out of bounds of array "Cod". You can allocate 30*11=330 bytes statically without a guilty conscience. And it will make the code faster.
It doesn't make sense to have 3 levels of indirection for an array of strings. You aren't even using the dynamic memory to hold a copy of strings, you just allocate pointers. This doesn't make any sense at all. If you need a pointer lookup table pointing at Cod then allocate it statically, it will only require sizeof(char*)*30 bytes.
As already mentioned, you can only use realloc on a pointer that has previously been malloc/calloc:ed.
As already mentioned, never typecast the result of malloc/realloc in C. This is C++ practice. In C, it destroys type safety and hides type compatibility bugs. There are countless, detailed discussions about this here on SO if you want to know the details.
What if you don't find "p" in the user string? The program will go havoc.
Don't name variables that affect fundamental program functionality to abstract things like tmp, vec etc. tmp could be renamed to input_buf or something, etc.
Avoid magic numbers in code, use const or #define for array length constants.
You can initialize strings in C, there is no need for strcpy to do so. char input_buf[INP_BUF_N] = "p";
To search for a char in a string, use strchr().
You shouldn't need to have the user inputting the same thing twice with scanf() in the outer do-while loop, likely a typo bug.
You can not do wild typecast between a static array of arrays to a pointer-to-pointer. This depends on the structure of whatever the pointer-to-pointer points at. Because a typical dumb-school-book dynamic 2D-array (malloc(Xsizeof(char)... malloc(Y*sizeof(char)) will not allocate memory adjacently. Plenty of discussions about this here on SO.
(you can allocate dynamic 2D arrays in adjacent memory with the use of array pointers or with "mangling", but those are rather advanced topics)
free() the dynamic memory once you are done using it.
As you hopefully can tell, the wise choice here is to rewrite this code from scratch.
Since it is a homework I tried to rewrite your code in something that should work...
char Cod[30][11];
char tmp[11];
char ***vec;
int i = 0;
strcpy (tmp, "p");
vec = (char***)malloc(sizeof(char *));
vec[0] = (char**)malloc(sizeof(Cod));
do {
scanf("%s", tmp);
int j = 0;
while(strcmp(tmp, "p")) {
strcpy(Cod[j], tmp);
scanf("%s", tmp);
j++;
}
vec = (char ***)realloc(vec, sizeof(char *) * (i+1));
vec[i] = (char **)malloc(sizeof(Cod));
memcpy(vec[i], Cod, sizeof(Cod));//you need to copy results since next time the Cod will be rewritten
scanf("%s", tmp);
i++;
} while((strcmp(tmp, "s")) == 0);
for three stars
char ***vec;
you need 3 mallocs (the casts are, at best, redundant in C and may hide an error)
vec = malloc(sizeof *vec);
vec[0] = malloc(sizeof *vec[0]);
vec[0][0] = malloc(30 * sizeof *vec[0][0]); /* sizeof (char) is 1 by definition */
In this 2 lines:
vec = (char ***) realloc (vec, sizeof ( char *) * (i + 1));
vec[i + 1] = (char ** ) realloc (vec[i + 1], sizeof ( char *) * (30));
If i = 1, then you reserve to "vec" i+1=2 pointers. In the second line you then call the 3rd one (vec[i+1] = vec[2] is 3rd element in table of size 2).