Passing the address of a dereferenced pointer to a string offset

Edenia

Assuming there is a function like this

int foo (char** str, int x)
{
    char* p = *str + x;

    foo2(&p); // declared as int foo2 (char** );
}

(oversimplified of course, the real function is recursive and much more complicated)


I've tried to do this:

int foo (char** str, int x)
{    
    foo2(&(*str + x));
}

But the compiler failed with error:

error: lvalue required as unary '&' operand

Why did the compiler shoot out with this error and how do I pass the pointer to a pointer to string x-byte(s) forwards, without declaring a variable and use its own address?


EDIT

Seems like there is some misunderstanding so I will post a complete simulation of what I want to achieve.

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

char* string = "This is a sample string.";
char* ptr;
int randomizer;

int receive_string (char* buffer, int size) // recv
{
    int i = 0;

    if(ptr == NULL)
        ptr = string;

    for(i = 0; *ptr != '\0' && i < size; ptr++)
    {
        if(randomizer == 2)
        {
            randomizer++;

            break;
        }

        buffer[i] = *ptr;

        i++;

        randomizer++;
    }

    if(*ptr == '\0')
    {
        buffer[i] = *ptr;

        i++;
    }

    return i;
}

int read_string (char* *buffer, int size, int alloc)
{
    int bytes = 0;

    printf("Reading string..\n");

    if(*buffer == NULL && alloc == 1)
    {
        printf("Allocating buffer..\n");

        *buffer = calloc(size, sizeof(char));
    }

    bytes = receive_string(*buffer, size);

    if(bytes == (-1))
    {
        return(-1);
    }
    if(bytes == 0)
    {    
        return 0;
    }
    if(bytes < size)
    {
        char* p = *buffer + bytes;
        //int temp = read_string(&p, size - bytes, 0); // works
        //int temp = read_string(&(char *){&(*buffer)[bytes]}, size - bytes, 0); // works
        int temp = read_string(buffer + bytes, size - bytes, 0); // doesn't work

        if(temp > 0)
            bytes += temp;
        else return bytes;
    }

    return bytes;
}

int main()
{
    char* buffer = NULL;

    int bytes = read_string(&buffer, strlen(string) + 1, 1);

    printf("[%u][%s]\n", bytes, buffer);

    if(buffer)
        free(buffer);

    return 0;
}

The randomizer is the dumbest quickie to "simulate" a recv() that can not receive all bytes. This implementation simulates recv() but instead of reading from a socket queue it reads from a global string.

0___________

if you want to pass the pointer to pointer to the particular char

foo2(&(char *){&(*str)[x]});

or

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Address of dereferenced pointer construct

From Dev

Passing a dereferenced pointer to a structure as a function argument

From Dev

Dereferenced pointer changes address in function call

From Dev

Passing dereferenced pointer (ie. by value) to a function expecting a pointer

From Dev

Passing the address of dereferenced smart pointers to functions that expect raw pointers

From Dev

passing a pointer address vs pointer to a pointer in C

From Dev

Segfault when assigning to indexed dereferenced pointer to string in c

From Dev

Adding 20 Byes Offset To Pointer Address

From Dev

C++ Read Memory Address / Pointer & Offset

From

Immutable string and pointer address

From Dev

Rust creating String with pointer offset

From Dev

Difference between passing “pointer to pointer” and “address of pointer” to a function

From Dev

Allocation of dereferenced pointer in Fortran

From Dev

Dereferenced pointer assignments

From Dev

Understanding C++ heap/stack allocations when passing dereferenced pointer by reference

From Dev

Passing a pointer to a function and change the address it points to

From Dev

passing array address in a function and returninf a pointer

From Dev

problem passing a pointer to function and return with unexpected address

From Dev

Taking the address of a c++ reference for passing to a pointer

From Dev

Passing destination string as a pointer in strcpy

From

Passing pointer to string array in Golang

From Dev

Passing String Pointer Array in C

From Dev

Passing a pointer (string) to a C function

From Dev

Passing default string pointer to function

From Dev

Assigning Address of String Literal to a pointer

From Dev

passing a double pointer into a function and assigning it an address of a pointer local to a function

From Dev

how is the address changed when passing a pointer to a pointer in function

From Dev

C++ read memory address with pointer+offset

From Dev

Finding pointer with 'find out what writes to this address' strange offset

Related Related

  1. 1

    Address of dereferenced pointer construct

  2. 2

    Passing a dereferenced pointer to a structure as a function argument

  3. 3

    Dereferenced pointer changes address in function call

  4. 4

    Passing dereferenced pointer (ie. by value) to a function expecting a pointer

  5. 5

    Passing the address of dereferenced smart pointers to functions that expect raw pointers

  6. 6

    passing a pointer address vs pointer to a pointer in C

  7. 7

    Segfault when assigning to indexed dereferenced pointer to string in c

  8. 8

    Adding 20 Byes Offset To Pointer Address

  9. 9

    C++ Read Memory Address / Pointer & Offset

  10. 10

    Immutable string and pointer address

  11. 11

    Rust creating String with pointer offset

  12. 12

    Difference between passing “pointer to pointer” and “address of pointer” to a function

  13. 13

    Allocation of dereferenced pointer in Fortran

  14. 14

    Dereferenced pointer assignments

  15. 15

    Understanding C++ heap/stack allocations when passing dereferenced pointer by reference

  16. 16

    Passing a pointer to a function and change the address it points to

  17. 17

    passing array address in a function and returninf a pointer

  18. 18

    problem passing a pointer to function and return with unexpected address

  19. 19

    Taking the address of a c++ reference for passing to a pointer

  20. 20

    Passing destination string as a pointer in strcpy

  21. 21

    Passing pointer to string array in Golang

  22. 22

    Passing String Pointer Array in C

  23. 23

    Passing a pointer (string) to a C function

  24. 24

    Passing default string pointer to function

  25. 25

    Assigning Address of String Literal to a pointer

  26. 26

    passing a double pointer into a function and assigning it an address of a pointer local to a function

  27. 27

    how is the address changed when passing a pointer to a pointer in function

  28. 28

    C++ read memory address with pointer+offset

  29. 29

    Finding pointer with 'find out what writes to this address' strange offset

HotTag

Archive