C programming in Linux warning message
-
Hello everyone. Im not quite sure either Im in the right forum since my question is basically on C programming, coding in the linux environment. After I gcc, I got this warning message "warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast" When I run the program, I will have a segmentation error fault. This is the code snippet below.
static const char *opt_socket_name[5];
main()
{
int i = 0;strcpy(*opt_socket_name[i], "testing")
printf("%s", opt_socket_name);
}Im not quite sure why this happen. Can anyone here help me and explain what is going on behind the scene. Can you guys also give an opinion on how to make it work. A brief on what I am doing is actually, creating multiple socket with different names. I am just about to create different names, but the warning above appeared. Is there any other options to do this? The above code is just a small testing code to make the strcpy() works. Im quite new in programming so currently im learning. :-D Thanks
-
Hello everyone. Im not quite sure either Im in the right forum since my question is basically on C programming, coding in the linux environment. After I gcc, I got this warning message "warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast" When I run the program, I will have a segmentation error fault. This is the code snippet below.
static const char *opt_socket_name[5];
main()
{
int i = 0;strcpy(*opt_socket_name[i], "testing")
printf("%s", opt_socket_name);
}Im not quite sure why this happen. Can anyone here help me and explain what is going on behind the scene. Can you guys also give an opinion on how to make it work. A brief on what I am doing is actually, creating multiple socket with different names. I am just about to create different names, but the warning above appeared. Is there any other options to do this? The above code is just a small testing code to make the strcpy() works. Im quite new in programming so currently im learning. :-D Thanks
It looks like you're trying to copy text into a string, but there are quite a few mistakes in the code: 1. A string is an array of characters, for eg. char str[100]. Therefore opt_socket_name[5] is a string that can hold 4 characters (+1 for the null terminator) 3. A * represents a pointer - you can also use a character pointer as a string, but it would point to an existing array of characters. 4. You use either * or an array. In your case, *opt_socket_name[5] means an array of 5 strings. 5. const = constant. Don't use that unless you don't want to modify the string. So here's what your code should look like:
static char opt_socket_name[20];
main()
{
strcpy(opt_socket_name, "testing");
printf("%s", opt_socket_name);
}If you're looking for an explanation for your warning and the segmentation fault, here it is: as I mentioned, in your original program, opt_socket_name[i] is a string and *opt_socket_name[i] points to a single character in the string. What you are passing to strcpy is a single character, which in C can be implicitly typecast to an integer, which in turn is typecast to a pointer and hence the warning.
-
It looks like you're trying to copy text into a string, but there are quite a few mistakes in the code: 1. A string is an array of characters, for eg. char str[100]. Therefore opt_socket_name[5] is a string that can hold 4 characters (+1 for the null terminator) 3. A * represents a pointer - you can also use a character pointer as a string, but it would point to an existing array of characters. 4. You use either * or an array. In your case, *opt_socket_name[5] means an array of 5 strings. 5. const = constant. Don't use that unless you don't want to modify the string. So here's what your code should look like:
static char opt_socket_name[20];
main()
{
strcpy(opt_socket_name, "testing");
printf("%s", opt_socket_name);
}If you're looking for an explanation for your warning and the segmentation fault, here it is: as I mentioned, in your original program, opt_socket_name[i] is a string and *opt_socket_name[i] points to a single character in the string. What you are passing to strcpy is a single character, which in C can be implicitly typecast to an integer, which in turn is typecast to a pointer and hence the warning.
yeah, That is basically what is I intended to do. I understand with the code you gave me since it is a basic strcpy() syntax. Anyway, thanks for the explanation behind the scene because that is the important thing i want to know. however, im not quite sure about what you mentioned of *opt_socket_name[5] means an array of 5 strings.. So does it means that *opt_socket_name[5] points to 5 strings? What I mean is like this :- *opt_socket_name[0] is pointing to a string for example char string1[10]; *opt_socket_name[1] is pointing to a string example char string2[10]; *opt_socket_name[2] is pointing to a string for example char string3[10]; *opt_socket_name[i] is pointing to a string for example char stringi[10]; How can i make a program to test this if my understanding here is true. The test is just to secure my knowledge and understanding. Thank a lot.
-
yeah, That is basically what is I intended to do. I understand with the code you gave me since it is a basic strcpy() syntax. Anyway, thanks for the explanation behind the scene because that is the important thing i want to know. however, im not quite sure about what you mentioned of *opt_socket_name[5] means an array of 5 strings.. So does it means that *opt_socket_name[5] points to 5 strings? What I mean is like this :- *opt_socket_name[0] is pointing to a string for example char string1[10]; *opt_socket_name[1] is pointing to a string example char string2[10]; *opt_socket_name[2] is pointing to a string for example char string3[10]; *opt_socket_name[i] is pointing to a string for example char stringi[10]; How can i make a program to test this if my understanding here is true. The test is just to secure my knowledge and understanding. Thank a lot.
You're partially right about the *opt_socket_name[5] thing. You declare the array of pointers using a *, eg. char *opt_socket_name[5]. Now, for a normal pointer, you would assign it the address of an existing variable. Example:
int value = 10;
int *pointer = &value;
printf("%d\n", *pointer);But the problem is that there is no string data type in C. A string itself is an array of characters and an array is internally a pointer, which means that a string is already a pointer. So the & and * are not used at all. This is how you would use an array of strings:
main() {
int i;
char *opt_socket_name[5];char string1[10];
// ...
char string5[10];strcpy(string1, "hello 1");
// ...
strcpy(string5, "hello 5");opt_socket_name[0] = string1;
// ...
opt_socket_name[4] = string5;for (i = 0; i < 5; i++)
printf("%s\n", opt_socket_name[i]);
} -
You're partially right about the *opt_socket_name[5] thing. You declare the array of pointers using a *, eg. char *opt_socket_name[5]. Now, for a normal pointer, you would assign it the address of an existing variable. Example:
int value = 10;
int *pointer = &value;
printf("%d\n", *pointer);But the problem is that there is no string data type in C. A string itself is an array of characters and an array is internally a pointer, which means that a string is already a pointer. So the & and * are not used at all. This is how you would use an array of strings:
main() {
int i;
char *opt_socket_name[5];char string1[10];
// ...
char string5[10];strcpy(string1, "hello 1");
// ...
strcpy(string5, "hello 5");opt_socket_name[0] = string1;
// ...
opt_socket_name[4] = string5;for (i = 0; i < 5; i++)
printf("%s\n", opt_socket_name[i]);
}