ban
ban

Reputation: 685

C programming to increase the size of a string

So I wrote this code but it gives me the same answer everytime. I am increasing the memory allocated to the pointer in steps of 4 and then print the value.

#include <stdio.h>

int main(void) {
    int n=0;
    char *name = "hello";
    scanf("%d",&n);

    for(int i =0; i<n;i++){
        name += sizeof(int);
        printf("%d \n", (sizeof(&name)));
    }
    return 0;
}

can someone help me? I don't know whats wrong here. I don't need a different code, I just want to understand what's wrong with this.

Upvotes: 2

Views: 15295

Answers (4)

Bartlomiej Lewandowski
Bartlomiej Lewandowski

Reputation: 11190

Let's step through your code one by one:

char *name = "hello"; 

this create an array of chars 'h','e','l','l','o',0 and assignes the memory address of the first character to name

for(int i =0; i<n;i++){
    name += sizeof(int);
    printf("%d \n", (sizeof(&name)));
}

here you add to the name pointer the size of int, which increments this pointer by 4 each pass.

Since this is a char pointer, the pointer is incremented by 4 bytes - since sizeof(int) == 4

You cannot increase the size of your hello char array, since it is not a dynamic array.

If you wish to be able to resize the string, you should malloc and copy the chars to the bigger array.

Upvotes: 0

Sergey L.
Sergey L.

Reputation: 22552

Here are some explanations of what is happening.

#include <stdio.h>

int main(void) {
    int n=0;

        // this does not actually allocate any memory. It sets the POINTER name to point (like an arrow) to a read-only block that contains "hello"
    char *name = "hello";
        // string literals generally come in fixed read-only memory

    scanf("%d",&n);

    for(int i =0; i<n;i++){
            // this causes the pointer memory address to be incremented by sizeof(int) (typically 4)
            // after the first increment if it will point to a string "o" (incremented by 4 characters)
            // after the second increment it will point to some undefined memory behind "hello" in your virtual address space and will have undefined behaviour when accessed
        name += sizeof(int);

            // sizeof(&name) will give you the size of a char **. Pointer to a character pointer. 
            // Wich is the same size as all pointers.
            // = sizeof(void *) = 8 for 64-bit systems, 4 for 32-bit systems
        printf("%d \n", (sizeof(&name)));
    }
    return 0;
}

This is the way to do it:

#include <stdio.h>

int main(void) {
    int n=0;

    // allocate 10 bytes of memory and assign that memory address to name
    char *name = malloc(10);
    // the size of that memory needs to be kept in a separate variable
    size_t name_length = 10;

    // copy the desired contents into that memory
    memcpy(name, "hello", sizeof("hello"));

    scanf("%d",&n);

    for(int i =0; i<n;i++){

        // reallocate the memory into something with sizeof(int) more bytes
        void * tmp = realloc(name, name_length += sizeof(int));
        // this can fail
        if (tmp) {
            name = tmp;
        } else {
            perror("realloc");
            exit(-1);
        }

        printf("%d \n", name_length);
    }
    return 0;
}

Upvotes: 2

mjk
mjk

Reputation: 541

Try the following, error checking was left out for clarity:

#include <stdio.h>
int main(void) 
{
    int n=0;
    char *name = null;
    scanf("%d",&n);

    for(int i=0; i<n;i++)
    {
        char *buffer = null;
        //allocate/reallocate the buffer. increases by 4 bytes every iteration
        buffer = (char*) realloc(name, (i+1)*4);
        name = buffer;
        printf("%d \n", (sizeof(&name)));
    }
    //release the memory used by the buffer
    free(name);
    return 0;
}

Upvotes: 5

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70989

You have not allocated any memory for the pointer at all in the code you provide. You will have to deal with dynamic memory if you want to change the size of the allocated chunk. You will have to initially use malloc and then use realloc to allocate more memory on each step.

Upvotes: 0

Related Questions