Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strcat String & Int Value

Status
Not open for further replies.

sleuth

Programmer
Jan 12, 2001
134
0
0
US

How can I combine a char type string with an int value.

I have a string with the value of "table_", which I defined like this:

char table[] = "table_";

And I have an int value of a random number, six digits.

I tried: strcat(table,number);

But I get errors like incompatible pointer types and so forth. I have no problem combining two char types, but I'm having no luck in combining a char & int type.

Perhaps someone could please point me in the right direction.

I'm from the perl crowd and I'm finding the transition to c a little easier than perhaps the next newbie, I should have no problem understanding a complex code example.

Thank you very much to all you c folks.

Also, is there a good site that I could go to for information on c.

And I'm on a linux server using gcc compiler.

Tony
 
Hi Tony. I somewhat started backwards from you. I taught myself C/C++ first before my perl studies. When I began my Perl studies I was amazed at how versatile perl was with string handling vs. C. Below is a snippet of C Code to append a random long to a C style char string. ->

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define MAX_STRING 100
#define MAX_RAND 32767

int main() {

char sSomeString[] = &quot;table_&quot;;
char sTempString[MAX_STRING];
long randNum;
srand( time( NULL ) );


randNum = rand()%MAX_RAND;

sprintf( sTempString,&quot;%s%ld&quot;,sSomeString, randNum );

printf(&quot;%s&quot;, sTempString );


return 0;

}

Check the man pages for literature on sprintf(...).
It works like printf(), but except for printing out to STDOUT, it stores the output in a char buffer.

LiquidBinary.


mlg400@blazemail.com
 
Great, it works just great.

Thanks a lot mate.

Now that I've got them combining, unlike perl, I'm not able to use:

table_189061['A']=1;

sprintf( TableName,&quot;%s%d&quot;,Table, tableName);

printf (&quot;Random Table: %s\n&quot;,TableName['A']);

I get null instead, which makes perfect sense. But how on earth could I now use my combination to access the char array of the TableName?

Thanks a lot pal!

Tony

.. and the man page refference is very helpful, thank you. While I'm waiting for any reply you may give I'll be researching as well.
 
Tony: If you have an array you always have to index it with integers ->

char table_189061[3];

table_189061[0] = 'a'; //First element.
table_189061[1] = 'b'; //Second element. etc...
table_189061[2] = '\0'; //Null out string.

//To print out a value ->

printf( &quot;%s&quot;,table_189061 ); //Print out full string.

//And the output would of course be: ab


If you are going to add char's to the array like the code above, always remember to NULL out one passed the last char in your array. If you don't then printf(...) won't know where to stop outputting chars, and will probably go passed the arrays allocated memory. With calls to sprintf(...) and strcpy(...), you don't have to worry about this. Also, you might want to check out planet-source-code.com. It's a very nice source-code database. The best way to learn to code in a new langauge is to learn to read source-code from other programmers. They have C source for the win32 platfrom as well as Unix/Linux.

LiquidBinary.




mlg400@blazemail.com
 

Great resource, I've been having a hard time finding c help. Cgi-resources has been helpfull to me also, but I couldn't find anything on this particular question.

Sorry but I was a little undescriptive in telling you my problem. I was putting little snippets up there.

I actually managed to figure out that for a string you need to end it wit \0 and the thing about decaring it ahead of time with the char table_18906[20]; definition.

I've got a little book from Sams teach your self thing, it's a little helpfull but only in the broad aspects like all the other books I ever buy.

Ok, in detail, what I am trying to do in this particular problem, is I have a bunch of char arrays all ready to go and be hooked up.

I have one int array with all the table id numbers in it. 24 at the moment. Then I randomly pick a number between 1 and 24, then I was able to get the random table Id and thanks to you combine the string &quot;table_&quot; with the id number.

So far that is working just great, but now I need to take the table id number and access the char array for it with the same name.

since you know perl I'd be comfortable showing you what I mean in perl.

%tables=(
'0' => '5568987',
'1' => '5978971',
'2' => '4567892'
);

%table_5568987=(
'A' => 'firstValue',
'D' => 'SecondValue'
);

$num = int(rand(2));
$RandtableId=$tables{$num};
$tableName=&quot;table_$RandtableId&quot;;
# Now I'd be able to access the random table like this
%$tableName{'A'};

Ok, that's it.

So do you see what I'm trying to do? I figure since perl's interpreter is c that anything I can do in perl I could do in c. What do you think?

Thanks a lot mate, your time is extremly valued to me.

Tony
 
Tony: I must say that I am more comfortable coding in C/C++ then I am with perl at the level of my studies, but I tried to implement from what I gathered from your script into C code->



#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>


#define MAX_STRING 14
#define MAX_RAND 11
#define NUM_OF_TABLES 10


char *Return_Rand_String();
void Append_To_Table( char sTableNames[][MAX_STRING],
char *sString,
int iYIndex );

int main() {


char sTable[] = &quot;table_&quot;;
char sTemp[MAX_STRING];
char sTableNames[NUM_OF_TABLES][MAX_STRING];
srand( time( NULL ) );


for ( int i=0;i<NUM_OF_TABLES;i++ )
{

strcpy( sTemp,sTable );
strcat( sTemp,Return_Rand_String() );
Append_To_Table( sTableNames,sTemp,i );

}



for ( i=0;i<NUM_OF_TABLES;i++ )
printf( &quot;%s\n&quot;,sTableNames );


return 0;

}


void Append_To_Table( char sTableNames[][MAX_STRING],
char *sString,
int iYIndex) {

for ( int i=0;i<MAX_STRING;i++ )
sTableNames[iYIndex] = sString;

}



char *Return_Rand_String() {


switch ( (rand()%MAX_RAND) )
{

case 0 :return &quot;5568987&quot;;
case 1 :return &quot;5978971&quot;;
case 2 :return &quot;4567892&quot;;
case 3 :return &quot;5468787&quot;;
case 4 :return &quot;5933271&quot;;
case 5 :return &quot;1267392&quot;;
case 6 :return &quot;5978947&quot;;
case 7 :return &quot;9998971&quot;;
case 8 :return &quot;4507896&quot;;
case 9 :return &quot;5568223&quot;;
case 10 :return &quot;1234976&quot;;
default :printf(&quot;Invalid range.\n&quot;);
return &quot;\0&quot;;

}




}


//Just a simple prog to see if this is what you have so far.
//As you can see I skipped the changing of an int to a
//string, so that everything is done with strings.

As you can see, char string manipulation in C is not as trivial as it is in perl. All the random tables are to be stored in a 2-D array (list). ->

char sTableNames[NUM_OF_TABLES][MAX_STRING];

//This will allocate a 2-D array with 10 random
//tables, with each table string being 14 chars in length.

Of course you do not have to set the # of tables at compile time. You can always allocate your array on the Free Store at run time (Man Pages : malloc(...)).

Now one of your questions was ->

# Now I'd be able to access the random table like this
%$tableName{'A'};

I don't understand what you mean by this statement. You said: ->

So far that is working just great, but now I need to take the table id number and access the char array for it with the same name.

I don't understand why you can't just get the values with simple integer notation. Do you want to be able to access the random table by say a key value (ID #)? It sounds like you want to implement a hash table. I just have 2 questions...

Do you want the user to enter in the ID#, then the prog displays the random table?

Or do you want to use the ID# to access the random table in the code?


Let me know if I am somewhere in the ballpark with this one.

Also: I am not too confident with perl yet, is => just a list separator? Same as a comma?

LiquidBinary.

mlg400@blazemail.com
 
Basically I think you know what I'm trying to do,

By the way, I took the code you posted and I'm trying to compile it right now to no avail, I'm getting errors, is it straight c or c++, I named the file test.c and test.cpp, cpp gave less errors though. I have

gcc version egcs-2.91.66 19990314 (egcs-1.1.2 release)

Should I get a later version?

I had a really hard time trying to update gcc the first time I tried about 6 months ago, but I'm a lot more savy with the teminal now even though I've still got a ways to go.

So about what I'm trying to do, as easily as I can lay it out I will explain furthur:

Lets say I have 10 arrays. They are char types. Each of the 10 arrays I will reffer to as a table.

I have 1 int type array which has 10 sets of 7 digit numbers.

int table_names[10];
table_names[0]=1234567;
table_names[1]=1234568;
table_names[2]=1234569;
table_names[3]=1234570;
table_names[4]=1234571;
table_names[5]=1234572;
table_names[6]=1234573;
table_names[7]=1234574;
table_names[8]=1234575;
table_names[9]=1234576;
table_names[10]=1234577;

Each of the 7 digit numbers in the array, is 'Inside' of the name of one of the ten tables.

The 10 tables are called out like the following example of a single table:

char table_1234567[20];
table_1234567['One']='TestValue';

I have each of my ten tables really simple like that example so while I'm trying to get it working I don't get confused.

Now that I have a random number between 1 and 10 (for the benifit of the examples I'll say 10 though I said 24 before) I select the random table Id from the table_names array.

With the random table Id in hand, I combine it with the string &quot;table_&quot; so I end up with:

table_1234567

Now that I have this value in a string called &quot;tableName&quot;

How can I access the table_1234567['One'] value using the tableName????

Now do you see what I mean? I'm really terrible at explaining things so I hope you've been able to bear with me.

I tried tableName['One'] but it prints out &quot;(Null)&quot;.

But if I print the tableName I get the name of the array I'm trying to access. So what modification could I make to get the access to the table?

Thank You!!!!!!!

Hey, after this do you want me to help you on any perl code? I'm really great for perl!

=> is used to assign the left name which is called the 'key' in perl to the right name the 'value' in the 'hash'. Hashes are a special feature of perl. And quite unique to perl. Although I think python and about 5 other langs also support them. Hashes are also reffered to as &quot;associative Arrays&quot;, which I read that c / c++ support too.

array['One']='two'; is the c equivilant I guess.

In perl it is

$array{'One'}='two';

Or

%array{
'One'=>'two',
'Two'=>'Three'
}
print &quot;$array{'Two'}&quot;;
# Output: Three

The % starts the hash, and is usually used to create the majority of the hash items at once.

Rather than adding each key to the hash one by one:

$array{'One'}='two';
$array{'Two'}='three';

.. and so on

then you can easily loop through the hash,

foreach $key (keys %array){
print &quot;$key - $array{$key}: &quot;;
}
# output: One - two: Two - three:

I actually used to be a moderator at the uh, perl guru forums. I still have status but don't post anymore since everyone thinks I'm an idiot there. There's a bunch of those double posters there or whatever. You know the ones, you'll answer someones question then the other guy posts after you and complains that your code is un-efficient and to use his instead.

A lot of people from there also post here too. Since perlguru.com is pretty much all perl newbies with perl questions. Smart folks there though.

Ok Mate, thanks again, I keep checking this thing from time to time.

You could ask me a favor anytime. I can do flash a little here an there, well I have flash5 so I could do some stuff you may like with it, and I am really good at perl & javascript. If you think about it, perl + java is really close to c.

Ok, I'm going back to work,

Tony
 
Ok, here's where I am:

I have an int array of tables,

int table_names[10];
table_names[0]=1234567;
//.. until table_names[10]

Then I randomly select one of the ten tables,

For the sake of keeping this as short as possible I'll say the random table number selected = &quot;1234567&quot;;

Ok, now I'm stuck.

I can either make my tables like this:

char table_1234567[50];
table_1234567['A']=500;
//.. and so on

then take the random number & combin it with the string &quot;table_&quot; to get

table_1234567

But then I can't use the combination to access the array with the same name. So I moved on an tried something else:

Defining the array like this would work if it was allowed,

table['1234567']['A']=500;

Then I could get the value of A in the 1234567 table from table['1234567']['A'], But It doesn't seem to work.


Thanks,

Tony
 
PS. I'm waiting for us to finish this thread so I can give you a tipmaster vote.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top