Solved

Cannot Convert 'Int' to 'Char *'

Posted on 1998-06-12
10
813 Views
Last Modified: 2011-04-14
How do you get a single word "Now is the time" from a file.
I manage to get a single character into an array. But when I try to strcat the array ie word[0][0] = "N" with word[1][0]= "o" to Temp[80]. I get the above message?? I don't understand both are char arrays. I need to concatenate the word[x][y] to temp ie. temp[0] = "Now"
                       temp[1] = "is"
                       temp[2] = "the"
                       etc...

Any assistance will be greatly appreciated.

Thanks


0
Comment
Question by:ss01
10 Comments
 

Author Comment

by:ss01
ID: 1165806
Edited text of question
0
 
LVL 2

Accepted Solution

by:
kellyjj earned 100 total points
ID: 1165807
I don't think you meant that you are storing the string in the 'x' element of the array.  With that in mind,  Here is how I would cat it.

strcat(temp,word[x]);

a char array set up like  word[50][80]  can be thought of as a array of 50 strings w/ lengths of 80.  

I hope this helps
0
 

Expert Comment

by:topkapi
ID: 1165808
any array char[50][80] is, as you might know, 2-dimensional.

You can strore a string "Now is the time" in a 1-dimensional array like this:
char String[80]
[N][o][w][ ][i][s][ ][t][h][e][ ][t][i][m][e][\0] ....

The rest of the [50] x [80] array is:
[e][m][p][t][y] ....
[m][ ][ ][ ][ ] ....
[p][ ][ ][ ][ ] ....
[t][ ][ ][ ][ ] ....
[y][ ][ ][ ][ ] ....
.......


0
 
LVL 1

Expert Comment

by:Booth882
ID: 1165809
if you let me see your code I will tell you what is going on
0
 
LVL 1

Expert Comment

by:Booth882
ID: 1165810
char ** TheCharPointers = new char * [4];

char * TempChar = new char[10];
TempChar[] = "now";
TheCharPointers[0] = TempChar;
TempChar = new char[10];
TempChar[] = "is";
TheCharPointers[1] = TempChar;

etc...

now each item in TheCharPointers is an individual word.  
you need to delete all those news when you are done if you do it this way:

for(int i = 0; i < 4; i++)
    delete [] TheCharPointers[i];

delete [] TheCharPointers;

I'm not sure if that's what you asked for, give me feedback
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:Booth882
ID: 1165811
the compiler gets confused sometimes because char and char * variables are kept as integer values and converted with a table.  A char is a one byte binary code, a char * is a hexadecimal address to a char or char array.  This may be what gave you the error message, and even though you say they are both char arrays this still happens sometimes.  You can change an integer into a corresponding char from the table by putting (char) in front of it, for example:

char TheChar = (char) 6;
or
int Spade = 6;
char TheChar = (char) Spade;

This makes TheChar a spade symbol.

Even though this mixup does happen sometimes, it is usually a signal that something else is wrong, because the ambiguity would not be arising if the variables were being properly used.  If I could see your code I could tell you exactly where your problem is.  
0
 

Expert Comment

by:jmelia
ID: 1165812
/*
// Reference: Kenigan and Ritchie

Hope this answers your question. Good luck with it..  
The problem is related to syntax. The use of character arrays is one area in 'C' where it is not always obvious to determine the difference between a pointer and a pointer to an address. Hopefully this code example may solve your problem .

Your question centered on 'C' code rahter than CStrings so the asnwer is oriented towards 'C' string, arrays, character pointers and byte addresses.

      char      tempArrayOfChars [80];     // a series of bytes  
      char      word [10][20];                 // 10 words, max of 20 chars per word

      memset (word, (char) NULL, sizeof (word));

      // Notice a string array is being copied to a string array
      strcpy (word [0], "Hello");
      strcpy (word [1], "Word");
      strcpy (word [2], "Append > ");
      strcpy (word [3], "Append > ");

      // Stick a byte into a specified address
      word [0] [0] = (char) 'x';            // Place value at the specified address
      word [0] [0] = (char) 'H';            // Place value at the specified address
      word [1] [1] = (char) 'X';            // Place value at the specified address
      word [1] [1] = (char) 'o';            // Place value at the specified address

      int offsetIntoArray = 9;
      char * pPtr_A = word [2];                                    
      pPtr_A += offsetIntoArray;      // A pointer to a desired location !
      strcpy (pPtr_A, "Here !  ");      // Append this text using a pointer to an address

                                                      //               //  Use the strcat function to do the same thing
      char * pPtr_B = word [3];                                    
      pPtr_B += offsetIntoArray;            // A pointer to a desired location !
      strcat (pPtr_B, "Another !! ");              // Append this text


      char * pSelectedWord = (char *) word [1];      
      strcpy (pSelectedWord, "Replacement");

      strcpy (tempArrayOfChars, pSelectedWord);
      strcat (tempArrayOfChars, pPtr_A);
      strcat (tempArrayOfChars, pPtr_B);


0
 
LVL 1

Expert Comment

by:Booth882
ID: 1165813
i get the feeling ss01 is not coming back
0
 

Author Comment

by:ss01
ID: 1165814
Jmelia and Booth882

Both of you get 50 pts. For the answer.

Thank you.

ss01

p.s. Sorry for not answering as I was away
0
 

Expert Comment

by:axefresh
ID: 8635405
hello guys,
Im designing a little program with MFC and Libnet.The interface has 4 edit boxes (among others): m_source_ip,(long)
              m_dest_ip,(long)
              m_src_port (short)
              m_dest_port.(short).

Meanwhile libnet exports a function called libnet_name_resolve and its prototype is:
       
       unsigned long libnet_name_resolve(unsigned char *,some macro);

This function returns an ip address in Network Byte Order of type unsigned long,wich i saved in src_ip and dest_ip .

Here´s part of the program

//------------------------------------  PROGRAM ENTRY ----------------------------------------------------
blah,blah,blah....

unsigned long src_ip,dest_ip; //source and destination ip Network Byte Order
unsigned short src_port,dst_port; //source and destination port

src_ip=libnet_name_resolve((unsigned char *)m_source_ip,LIBNET_RESOLVE);
dest_ip=libnet_name_resolve((unsigned char *)m_dest_ip,LIBNET_RESOLVE);

src_port=(unsigned short)m_src_port;
dest_port=(unsigned short)m_dest_port;

But...The program doesnt work.I used the debugger and a sniffer and the packets go out
with src_port=dest_port=0 and src_ip=dest_ip=255.255.255.255.Seems like the function isnt resolving nothing....

I tried this way and it worked.....

//------------------------------------  PROGRAM ENTRY ----------------------------------------------------
blah,blah,blah....

unsigned char s_ip[]="192.168.0.1",dst_ip[]="192.168.0.2";
unsigned long src_ip,dest_ip; //source and destination ip Network Byte Order
unsigned short src_port,dst_port; //source and destination port

src_ip=libnet_name_resolve(s_ip,LIBNET_RESOLVE);
dest_ip=libnet_name_resolve(d_ip,LIBNET_RESOLVE);

src_port=1025;
dest_port=80;

Using class wizard i have a limited range of data types and using type casting,atoi,htons,htonl,etc...I just cant make it to work....
I made this program in pure c and it worked....Probably its just a stupid mystake but i cant see it....

Hope someone can help me cos im stucked in this sh*t.

PS:Sorry about my English :)





 
 
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now