So my problem is that I get this message when I try to print the address (in decimal) of where the pointer points to.
"warning: format '%d' expects type 'int', but argument 2 has type 'char *' "
If my understanding of pointers are correct, when using printf,
ptr = prints the address of the where the pointer is pointing to
&ptr = prints the actual address of the pointer
*ptr = prints whatever value is held in the address
Here's a snippet of my program:
Code:
#include <stdio.h>
#include <stdlib.h>
void get_values(char *ptr, char *infile);
int main(int argc, char *argv[]) {
char text;
char* input=NULL;
int len=0;
/*checks if user has enter correct number of command line arguements*/
if(argc !=2) {
printf("\nNo input file!\n");
exit(1);
}
FILE *in = NULL;
if ((in=fopen(argv[1], "r"))==NULL)
printf("\nUnable to open file\n");
/*gets the number of characters in the file*/
while(!feof(in)) {
fscanf(in, "%c", &text);
len++;
}
fclose(in);
/*allocate memory for all characters in file*/
input = (char *) calloc(len, sizeof(char));
if (input == NULL) {
printf("Unable to allocate array");
}
get_values(input, argv[1]);
printf("\n %d, %c", input, *input);
printf("\n %d, %c", input+1, *(input+1));
return 0;
}
void get_values(char *ptr, char *infile) {
FILE *in = NULL;
if ((in=fopen(infile, "r"))==NULL)
printf("\nUnable to open file\n");
do{
fscanf(in, "%c", ptr);
ptr++;
}while(!feof(in)) ;
fclose(in);
}
Also, is it possible to do arithmetic with addresses, for example,
Code:
//lets say i have a character at address 18
//but I want to start my output at every 8th byte,
//so my output address will start at 16
n=address%8; //n=18%8=2
m=address-n;
I've tried doing exactly that, but it won't let me do that. I get the error "error: invalid perands to binary % (have 'char *' and 'int')".
Is there a way I can do this?