How to cast a char ptr to an array (to prevent warning, it does work)
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to cast a char ptr to an array (to prevent warning, it does work)
/* testarray.c - to test compile warnings/errors */
/* "warning: passing arg1 from incompatible type" */
/* - by Owen Townsend, UV Software, Aug 25/2008 */
/* - original program was 20,000 lines (uvcopy.c) */
/* so I wrote this small test program to experiment*/
/* I was trying to pass a char ptr to a function which expects an array ptr*/
/* I was getting warning "passing arg1 from incompatible pointer type" */
/* note - program works OK, but I would like to eliminate the warning msg */
/* - I tried cast (char[][100]) but get "error: cast specifies array type" */
/* This test program written to investigate problem */
/* - here are warnings/errors from this test program */
/* 34: warning: passing arg1 of 'wordsep1' from incompatible pointer type*/
/* 36: error: cast specifies array type */
/* 38: error: cast specifies array type */
/* 40: warning: passing arg1 of 'wordsep1' from incompatible pointer type*/
char line[1000]; /* string to be split into words array */
char words[9][100]; /* words isolated from line */
char words2[1000]; /* words declared as an area vs array */
char *(words2p[9][100]); /* ptr to array of 100 elements */
/* declare prototype for wordsep1() function */
int wordsep1(char words[][100], char *input, int max, short bits);
int main (int argc, char *argv[])
{
wordsep1(words,line,9,0x03); /*32 <-- OK (no warning or error) */
wordsep1(words2,line,9,0x03); /*34 warning: arg1 incompatible type */
wordsep1((char[][100])words2,line,9,0x03); /*36 error: cast array type */
words2p = (char[][100])&(words2[0][0]); /*38 error: cast array type */
wordsep1(words2p,line,9,0x03); /*40 warning: arg1 incompatible type */
return(0);
}
/*-------------------------- wordsep1 ----------------------------*/
/* separate input string into array of words */
int wordsep1(char words[][100], char *input, int max, short bits)
{
return(1);
}
first off, please put code inside code tags, it's really hard to read otherwise. Secondly, your question is easier to read when it's not in the code block.
That said, "char foo[]" is not the same as "char foo[][]" yet, you are trying to use them the same way. It may work because everything is static, but is actually wrong - hence the warnings/errors. (actually, I get all compile errors... what compiler are you using?)
There's no proper casting of a char** to a char* (or vice versa) - they simply aren't the same. You can create 2 different functions that take two sets of arguments even if they are called the same, i.e.:
Code:
#include <stdio.h>
void myfunc(int a) {
printf("int version: %d.\n",a);
}
void myfunc(float a) {
printf("float version: %f.\n",a);
}
int main() {
int a = 4;
float b = 4.4;
myfunc(a);
myfunc(b);
return 0;
}
outputs:
Code:
int version: 4.
float version: 4.400000.
so you could make a func that takes a char* (or char[]) and one that takes a char** (or char[][]), call them the same thing, then your example may work. You can even make one call the other after correctly converting the data if that would make coding easier.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.