Unclear pointer and array

Its good to know that you are happy ! I am sleeping now :slight_smile:

The error is clear enough. You are feeding a 'something *' into %c when it wants a 'something' without the *. Most of the various kinds of integers can be converted between, but %c does not print strings. It prints single characters, only single characters, and nothing but single characters.

To convert a 'something *' into a 'something', you either do (*something) or something[0] to dereference it. If 'something' was a string, these would get you the first character of it.

Thanks Corona!
What I meant panic is to match the format arguments of printf(), especially when pointers are involved. My bad part is the not-enough-understanding of the pointer, as I did not realize my wrong catches until I saw the warnings/errors.
Go back to the original post of this thread, First, I was wondering why array worked but pointer not. After the discussion I seemed to have some ideas that when array is used, the memory is automatically allocated, but memory was only allocated to the variable address if a pointer is used, such that enough memory needs to be allocated to the pointers of the string/substring.
The second question is what the correct way is if pointer is used. (Maybe using pointer is not the correct way, but that is another story.) But this topic was distracted by other problems with sprintf() and printf() format arguments like Don's tricks.
The most difficult parts with C to me are: 1) dynamic memory allocation of pointers, 2) match the format arguments of printf() when pointers are involved. That's what I am always panic with. The critical point is still the pointer manipulation. Need more read and study. Could not find a good course around that digs C deep as I want. Neither could I find a good instruction/book on this topic yet, or too many diverse information about this for me to catch the critical points, I should say. Thanks you lots any way. Any suggestion?

I think you're overthinking this quite a whole lot. You've divided this into a lot of separate categories like "the correct way to use pointers when malloc is involved", "the correct way to use pointers when printf is involved", "the correct way to use pointers when sprintf is involved", "the correct way to use pointers when strtok is involved", etc -- as if these things had nothing to do with each other.

If you look at them though, their arguments help give you an idea how they work.

$ man malloc

void *malloc(size_t size);

$ man strtok

char *strtok(char *str, const char *delim);

$ man strcpy

char *strcpy(char *dest, const char *src);

$ man sprintf

int sprintf(char *str, const char *format, ...);

$

malloc returns a 'void *'. It's not const, so the memory it returns is memory you're allowed to write to. 'void' means its of no particular type, that you're expected to cast it into the type you need. So malloc returns memory which you're supposed to cast into some other type, memory which you're allowed to write to.

strtok takes a 'char *', which means you're giving it memory that strtok's allowed to alter, as well as a 'const char *', memory it's not allowed to change -- the list of tokens. It returns a 'char *', a pointer to memory you're allowed to write to. (As it turns out, a pointer to the same memory, even.)

strcpy's arguments show you which one is src and which one is dest, even without their names -- the 'const char *' has to be the source because that's memory strcpy can't write to.

and sprintf takes a pointer to memory it can write to(the output string), memory it can't write to(the command string), and a variable number of arguments just like printf.

As for the way printf and sscanf demand pointers and non-pointers, there's nothing really deep to learn about C there, it's just the way the printf and scanf calls were built. They could have made printf demand everything be pointers, just like scanf does, but didn't.

Hai yifangt please do post your difficulties in forum as you posted in current thread, we shall discuss Don is there, Corona is there...many other experienced experts are there.. it's good for me also, I can recall "Let Us C" which I read during my college days... and many people who refer this forum also will learn..:slight_smile:

1 Like

I am reading this book too, but as self-study. That's why I have so many questions. Here is another one that I found at link which is a function to replace a substring from original string:

char *replace_str(char *str, char *orig, char *rep)
{
    static char buffer[1024];
    char *p;                             //Line 4: 
    if (!(p = strstr(str, orig)))        //Line 5: p is the position of first occurrence of 'orig' in 'str', i.e. 6 (real position maybe something as 5f0076) at "Hello world!"
            return str;                  //Line 6: 'str' is a string, returned if 'orig' not found
    }   
    strncpy(buffer, str, p - str);      //Line 8: Copy 'p-str' (6=5f0076 - 5f0070) characters from 'str'(where 'orig' starts at) to 'buffer', i.e. from the "start"(letter 'H') to the first occurrence of "orig" (letter 'w')
    buffer[p - str] = '\0';              //Line 9: put '\0' to the end of buffer, now '\0' is at the position letter "w" of world used to stay. buffer[] becomes "Hello \0".

    sprintf(buffer + (p - str), "%s%s", rep, p + strlen(orig)); //Line 11: Now 'rep' is copied to buffer, buffer[] becomes "Hello Miami" but what about p+strlen(orig)?????
    return buffer;
}

I put my understanding at the end of each line. Line 11 bugs me again, which is closely related to previous pages of this thread. The question is: what is the pointer movement for "p + strlen(orig)" to "buffer+(p-str)" ?
In another way, how 'rep' and 'p+strlen(orig)' is concatenated/combined?
The author used Hello world! to Hello Miami! as example. Thanks a lot!

---------- Post updated at 12:21 PM ---------- Previous update was at 12:04 PM ----------

Ah ha!
p+strlen(orig) is ' ! ', i.e. the rest of the string after the match 'world', here is the exclamation mark ' ! '. Am I right?

Yes, you are correct. That is a pretty convoluted example. I'd have done the pointer arithmetic in a separate statement, like char *rest=p+strlen(orig); // Go past the original word for the rest of the string

1 Like

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.

Here is my 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:

$ 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!

You must be on a 64-bit system. The way those have mostly worked out is that 'int' remains 32-bit, 'long int' becomes 64-bit, and pointers become 64-bit. On a 32-bit system, int, long int, and pointers are all 32-bit.

Subtracting two pointers from each other gives you a 64-bit integer, but %d expects a 32-bit integer. I think that would end up printing 0 for all numbers less than the 32-bit limit.

I would always use %ld to print pointer differences, just to be safe, since it'll work properly on both 32 and 64 bit systems.

1 Like

Great! that's what I suspected and now confirmed. Thanks a lot. Have a nice day!