LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 09-03-2008, 06:08 PM   #1
Owen_Townsend
LQ Newbie
 
Registered: Dec 2004
Location: Vancouver Canada
Distribution: RHEL 3.0
Posts: 7

Rep: Reputation: 0
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);
}
 
Old 09-03-2008, 09:14 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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.

hope that helps.

Last edited by BrianK; 09-03-2008 at 09:16 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] gcc Warning: Array index has type 'char' traene Programming 4 04-20-2008 08:43 PM
warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast nasim751 Programming 2 02-10-2008 10:47 PM
warning: passing argument 2 of ‘memset’ makes integer from pointer without a cast nasim751 Programming 3 01-30-2008 09:16 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
search in char array xxfunkxx Programming 2 12-12-2004 11:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration