Thanks Corona!
Your way is clearer and easier to understand. I have to admit that some programmer like to use tricks so that I had hard time to understand. However their code is very concise with lots information hiding behind. I was warned better not do that way, but actually I admire them very much as I think you can do that only you have really good catch of the idea.
About the warnings I panic at, here is an example that I had thought my code is fine while I tried another program to do the calculation of pointer positions.
Quote:
warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘size_t *’ [-Wformat] etc.
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Try the calculation of char pointers cf. array subscription
// I did not figure out the char pointer can be implicitly converted to integer number
//
int main()
{
char str[] = "The quick brown fox jumps over the lazy dog!";
char *line;
char *p, *q, *l, *m;
line = (char *) malloc(sizeof(str)); //Line 15 allocate memory
strcpy(line, str); //Line 17 copy char array to char pointer
p = strstr(line, "fox"); // Find the position of "fox" in line
q = strstr(line, "dog"); // Find the position of "dog" in line
l = strstr(line, "quick"); // Find the position of "quick" in line
m = strstr(line, "lazy"); // Find the position of "lazy" in line
printf("Postion subtraction q(%p) - p(%p) = %d\n", p, q, q - p); //Line 25
printf("Postion subtraction m(%p) - l(%p) = %d\n", m, l, m - l); //Line 26
printf("Char value subtractn: o(%c) - q(%c) = %d\n", str[12], str[4], str[12] - str[4]);
printf("Char Value subtractn: q(%c) - e(%c) = %d\n", str[4], str[2], str[4] - str[2]);
printf("Position subtraction: &q - &e = %d\n", &str[4] - &str[2]); //Line 30
return 0;
}
and the warnings are:
Code:
$ gcc -Wall ptr_substraction001b.c
ptr_substraction001b.c: In function ‘main’:
ptr_substraction001b.c:25:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]
ptr_substraction001b.c:26:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long int’ [-Wformat]
ptr_substraction001b.c:30:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat]
$ ./a.out
Postion subtraction q(0x987020) - p(0x987038) = 24
Postion subtraction m(0x987033) - l(0x987014) = 31
Char value subtractn: o(o) - q(q) = -2
Char Value subtractn: q(q) - e(e) = 12
Position subtraction: &q - &e = 2
And I changed to %ld according to the warnings, the warnings went away. Now, the point is I must have missed something before I saw the warnings. Why should I use %ld, or, why the pointer address subtraction should be long int? Thanks a lot!
void main()
{
int a={1,2,3,4,5,6,7,8,9,10};
int *p=a;
int *q=&a;
cout<<q-p+1<<endl;
}
The output is 10, how?
if we give cout<<q it will print the address, value won't print....
if we give cout<<p it will print the address, value won't print....
p has the base addr; q... (1 Reply)
All ..
I am having a pointer array . And trying to store the addess into that pointer array . please see below the problem i faced
code:
int cnt1;
char *t_array;
char *f_array;
for(cnt1=0; cnt1<1000; cnt1++)
{
t_array =... (1 Reply)
If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it:
(1) one is to pass a pointer-to-pointer parameter, like:
int my_malloc(int size, char **pmem)
{
*pmem=(char *)malloc(size);
if(*pmem==NULL)... (11 Replies)
Hi all,
Can anyone provide help with getting the right syntax regarding array/pointers in C in the following code? Can't locate a specific example which clarifies this...
Say I declare a typedef to an array of pointers to some type...
/**
* An array of ptrs to sections
*/
typedef... (4 Replies)
if i create an array of pointers to a structure "struct node" as:
struct node *r;
and create "n" number of "linked lists" and assign it to the various struct pointers r using some function with a return type as structure pointer as:
r=multiplty(.......) /*some parameters*/
is... (2 Replies)
Hi guys,
Besides the points bellow, what would best practices for scripting be ?
1) set the PATH
2) unset the current environment (set -u ?)
3) (re)set the IFS to default value - space (IFS="" <- is this correct ?)
4) check the return code for each action inside the script (cd, rsync,... (1 Reply)
I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y, especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here... (1 Reply)