Hello!

I am fairly new to C, and trying to learn to deal with malloc.

The objective for the code, is to allocate memory in order to store a string on the heap.

Compiler says "Warning: assignment makes integer from pointer without a cast"
"Warning: format '%s' expects type 'char *', but argument 2 has type 'int'"
"Warning: format '%s' expects type 'int', but argument 3 has type 'char *'"

All these 3 warning occurs 2 times each, of course.
I have read that the first warning is something you get if you forget to #include stdlib, which as you can see, I have not.

Seems to me I'm just being stupid, but I have been bangin my head on this one for a while now.

Using code::blocks IDE with GNU GCC.

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


void heapstrings(void)
{


    char *p1 = malloc(sizeof(char[13]));
    char *p2 = malloc(sizeof(char[13]));


    *p1 = "Clydefrog";
    *p2 = "Cartman";


    printf("p1 is pointing on string %s at adress %d\n",*p1, p1);
    printf("p2 is pointing on string %s at adress %d\n",*p2, p2);

}


int main()
{
    heapstrings();
    printf("Hello world!\n");
    return 0;
}

Cheers

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


void heapstrings(void)
{


    char *p1 = (char*)malloc(sizeof(char[13]));
    char *p2 = (char*)malloc(sizeof(char[13]));

//check here to see if malloc failed.

    strcpy(p1, "Clydefrog");
    strcpy(p2, "Cartman");


    printf("p1 is pointing on string %s at adress %p\n", p1, (void*)p1);
    printf("p2 is pointing on string %s at adress %p\n", p2, (void*)p2);

}


int main()
{
    heapstrings();
    printf("Hello world!\n");
    return 0;
}

Get yourself a decent book on C programming - The C Programming Language by Brian W. Kernighan, Dennis Ritchie, comes to mind.

I have the wikibooks version of "The C programming language", but I did not manage to figure this one out. I am actually learning C++, and I have used C before only in the context of embedded systems, so I havent had any need for the use of malloc before.

In my C++ primer, I have the following text:

string s ("some value");

string *sp = &s;       //sp holds the adress of s
cout << *sp;           //prints some value

*sp = "a new value";   //contents of s now changed

So assigning a string directly to the dereferenced pointer doesnt work when using malloc, ok.

Anyway, thanks alot!

I have the wikibooks version of "The C programming language", but I did not manage to figure this one out. I am actually learning C++, and I have used C before only in the context of embedded systems, so I havent had any need for the use of malloc before.

In my C++ primer, I have the following text:

string s ("some value");

string *sp = &s;       //sp holds the adress of s
cout << *sp;           //prints some value

*sp = "a new value";   //contents of s now changed

So assigning a string directly to the dereferenced pointer doesnt work when using malloc, ok.

Anyway, thanks alot!

The problem with your original code is, your allocating memory and assigning the pointer to the allocated memory to the pointers p1 and p2 and then you promptly assign the string literals "Clydefrog" and "Cartman" to the pointers p1 and p2...which creates a memory leak.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.