C programming and "include"

Hmm, so you can't link the files with code inside a c file? You must do it through the compiler? I tried gcc -c -Wall filename.c and it gave me a warning: int format, pointer arg (arg 2) for this line

printf("%c", myarray[j]);
 
KataKoniK|ATI said:
Hmm, so you can't link the files with code inside a c file? You must do it through the compiler? I tried gcc -c -Wall filename.c and it gave me a warning: int format, pointer arg (arg 2) for this line

printf("%c", myarray[j]);

technically you could. but... it's bad form so I won't discuss it any further.
you must compile all your source files in one line, otherwise the linker will get confused. look at posts above.
 
seeker010 said:
technically you could. but... it's bad form so I won't discuss it any further.
you must compile all your source files in one line, otherwise the linker will get confused. look at posts above.

If I wanted to, what code would I add? I tried compiling all the source files on one line and it gave me that same warning:

warning: int format, pointer arg (arg 2) for this line

printf("%c", myarray[j]);

When I do ./a.out, it prints a bunch of garbage characters.
 
KataKoniK|ATI said:
If I wanted to, what code would I add? I tried compiling all the source files on one line and it gave me that same warning:

warning: int format, pointer arg (arg 2) for this line

printf("%c", myarray[j]);

When I do ./a.out, it prints a bunch of garbage characters.

bah. just include the c file. don't say I didn't warn you. it adds all sorts of problems in complex code.
%c is for a single character. didn't you define myarray as char* myarray[j]? if not ignore the following.
that creates a matrix of pointers to chars. in other words each element contains a pointer, so when you're outputting, you're saying output as char the address of the character array.
try %s instead.
 
Last edited:
Worked, thanks. One last question. How do you convert a char to an int in C?

Specifically, I am having trouble getting the following to work:

char stuff[] = {"1","2","3"};

for(i = 0; i < 3; i++) {
printf("%s", randomarray[stuff]);
}
return 0;

I know I can just refer to i instead of stuff, but I want to learn how would I do this in C because it's really easy to do in Java.
 
Last edited:
KataKoniK|ATI said:
Worked, thanks. One last question. How do you convert a char to an int in C?

Specifically, I am having trouble getting the following to work:

char stuff[] = {"1","2","3"};

for(i = 0; i < 3; i++) {
printf("%s", randomarray[stuff]);
}
return 0;

Here:

char stuff[] = {"1","2","3"};

for(i = 0; i < 3; i++) {
printf("%s", randomarray[atoi(stuff)]);
}
return 0;

there are a number of functions of this sort.
atoi()
atol()
atof()

KataKoniK|ATI said:
I know I can just refer to i instead of stuff, but I want to learn how would I do this in C because it's really easy to do in Java.


No problem. Ask away.

I'm learning Java right now so I may have some quesitons back sometime. I've been doing embedded C for almost 7 years now.
 
I gave the atoi a try and it gave me a warning that passing arg 1 of atoi makes pointer from integer without a cast. Any idea what the problem maybe?


Yeah, Java is pretty fun. I had a great time using it to program stuff. I'm slowly starting to develop a hate for C lol in terms of ease of coding certain things
 
KataKoniK|ATI said:
I gave the atoi a try and it gave me a warning that passing arg 1 of atoi makes pointer from integer without a cast. Any idea what the problem maybe?


Yeah, Java is pretty fun. I had a great time using it to program stuff. I'm slowly starting to develop a hate for C lol in terms of ease of coding certain things
Defilne 'stuff' as array of char pointers:

char *stuff[] = {"1","2","3"};

edit: with plain char array you get three characters (1 byte signed integers): '1', '2', '3'. With pointer array you get three strings: "1", "2", "3" and atoi needs char* (~string) as input argument.
 
KataKoniK|ATI said:
I gave the atoi a try and it gave me a warning that passing arg 1 of atoi makes pointer from integer without a cast. Any idea what the problem maybe?


Yeah, Java is pretty fun. I had a great time using it to program stuff. I'm slowly starting to develop a hate for C lol in terms of ease of coding certain things
ah, yes I know.

Those are chars, not strings. I looked quick and thought they were strings.
Change the decl to:
char *stuff[] = {"1","2","3"};
you could do it with chars, but you'd be limited to single digit numbers.

If you want to keep it as chars and don't care about more than single digit then the proper init is:
char stuff[] = {'1','2','3'}; //single quotes.

then do this:
printf("%s", randomarray[stuff-'0']);
It will look at the ACII value of '0' and substract it from stuff.
 
Alright, quick question about File I/O in C. Check this example:

http://cplus.about.com/od/beginnerctutoria1/l/aa042902c.htm

Is it possible to edit on the fly the file, lesson13_in?

For example,

I code something that opens and reads lesson13_in. If the line being read in lesson13_in contains "john", change "john 12345" to "suzy 12345", so lesson13_in in the end will only have "suzy 12345" in it. How would you achieve this? Does this make sense?

Thanks again.
 
noodles said:
My guess, Zenitram is that you're working on an embedded processor. Right?
yep. I dislike c very much. My first time using it. I miss the flexibility of C++, classes and scope! Technically C should be more flexible but my mind doesnt think like that!
 
KataKoniK|ATI said:
Alright, quick question about File I/O in C. Check this example:

http://cplus.about.com/od/beginnerctutoria1/l/aa042902c.htm

Is it possible to edit on the fly the file, lesson13_in?

For example,

I code something that opens and reads lesson13_in. If the line being read in lesson13_in contains "john", change "john 12345" to "suzy 12345", so lesson13_in in the end will only have "suzy 12345" in it. How would you achieve this? Does this make sense?

Thanks again.
Well this is the more common approach to this. Read in the file and store all the data. For simple data an array would do or if it's detailed data you might want to make a struct. Then change/add/delete the data structure, do whatever, and call something to spit the data back out to the file. Just don't do it in a loop or anything like that, trust me. =)
 
Thanks. How do you make a printf statment extend to two lines? Because I have one now that's more than 80 lines.

I want to do the following

Code:
printf("stuff stuff stuff stuff stuff stuff
               stuff stuff stuff %s\n", variable);
 
KataKoniK|ATI said:
Thanks. How do you make a printf statment extend to two lines? Because I have one now that's more than 80 lines.

I want to do the following

Code:
printf("stuff stuff stuff stuff stuff stuff
               stuff stuff stuff %s\n", variable);

Not exactly sure what you are asking for but to do line breaks just add "\n" wherever appropriate. So printf( "hi\nbye" ); would display as:

hi
bye

on the console. If you mean in code, then just do multiple printf statements. Not the most efficient but itll get it done for ya. OR, you could build a string and then used one printf statement to display the string. If you know anything about dynamic allocation or are using a string class you can do this easily. So instead of a whole bunch of printf statements you would have a bunch of statements that are building onto an existing string and then one printf statment to display the built up string. Just be sure to delete everything you new so you don't leak nub! ;)

Have fun!
 
KataKoniK|ATI said:
Thanks. How do you make a printf statment extend to two lines? Because I have one now that's more than 80 lines.

I want to do the following

Code:
printf("stuff stuff stuff stuff stuff stuff
               stuff stuff stuff %s\n", variable);
If you want to prevent the source code to exceed 80 characters in width, you can use the following:
Code:
printf("stuff stuff stuff stuff stuff stuff stuff stuff "
     "stuff stuff stuff stuff stuff stuff stuff stuff "
     "stuff stuff stuff stuff stuff stuff stuff stuff ");
 
noodles said:
ah, yes I know.

Those are chars, not strings. I looked quick and thought they were strings.
Change the decl to:
char *stuff[] = {"1","2","3"};
you could do it with chars, but you'd be limited to single digit numbers.

If you want to keep it as chars and don't care about more than single digit then the proper init is:
char stuff[] = {'1','2','3'}; //single quotes.

then do this:
printf("%s", randomarray[stuff-'0']);
It will look at the ACII value of '0' and substract it from stuff.


You could try this:
unsigned char stuff[] = {0,1,2,3,...,255}; //No Quotes. Old cheap way to store small numbers

then do this:
printf("%s", randomarray[stuff]);
 
Back
Top