pointer question

J

Jordan Taylor

#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
}

int main()
{
char *c;
f(&c);
printf("%s\n", *c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?
 
J

John Tsiombikas (Nuclear / Mindlapse)

#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);

Do not cast the result of malloc, just include stdlib.h for its
prototype. The way you did it, the compiler thinks that malloc returns
an int (which is the default assumption in the absence of a prototype),
and in case your pointer size is not the same with the size of int, as
is the case with the machine I'm using to write this right now, you
*will get in trouble*.

Also, sizeof(char) is by definition 1, so it's not necessary here.
}

int main()

int main(void)
{
char *c;
f(&c);
printf("%s\n", *c);

*c gives a char (the first character of your array), while the %s format
specifier expects a string (char*).
The correct call would be:

printf("%s\n", c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?

again the *c is wrong, also, use strcpy() or strncpy() to copy strings:
strcpy(c, "hello");
 
I

ian

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

void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
memcpy (*c, "hello", 6);
}


int main()
{
char *c;
f(&c);
printf("%s\n", c);
return 0;
}

c is a pointer. You can't use *c in printf().
 
R

Richard Heathfield

Jordan Taylor said:
#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
}

int main()
{
char *c;
f(&c);
printf("%s\n", *c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?

No, you forgot to prototype malloc. The cast didn't help, since it may well
have obscured this fact. And your print is wrong, too.

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

void f(char **c)
{
*c = malloc(sizeof **c * 6);
if(*c != NULL)
{
memcpy(*c, "Hello", 6);
}
}
int main(void)
{
char *c = NULL;
f(&c);
if(c != NULL)
{
printf("%s\n", c);
free(c);
}
return 0;
}

Note the added headers, the allocation idiom P = malloc(sizeof *P * n), and
the corrected printf call. Note also the error checking and resource
release.
 
R

Richard G. Riley

"Jordan"posted the following on 2006-03-10:
#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
}

int main()
{
char *c;
f(&c);
printf("%s\n", *c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?

No : I'm assuming you are doing all this horrible indirect as a little
experiment? So we'll ignore nasty type things etc.

Try this instead:

memcpy(c,"Hello",6); // not *c
printf("%s\n", c); // not *c

Use of a debugger would have immediately shown you that *c was 0 or
some other "non value": ie it was the newly allocated memory
block contents as opposed to the char pointer you intended.

Good luck!
 
R

Richard Heathfield

John Tsiombikas (Nuclear / Mindlapse) said:
again the *c is wrong, also, use strcpy() or strncpy() to copy strings:
strcpy(c, "hello");

The strncpy function doesn't copy strings; it copies substrings (which may
happen to be strings, but needn't be).

The strcpy function is a perfectly reasonable thing to use to copy strings,
although if you do happen to know the length in advance it's entirely
possible that memcpy will be a fair bit quicker. Yes, strcpy expresses your
intent better - so it's probably better to use that unless performance is
really at an out-and-out premium and you're in an inner loop.
 
K

Keith Thompson

Jordan Taylor said:
#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
}

int main()
{
char *c;
f(&c);
printf("%s\n", *c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?

Aside from the other advice you've gotten, you use the name "c" for
the char** parameter to f() and for the char* variable in main().
That's bound to cause confusion.

You tell us that you tried memcpy(), but you didn't actually show us a
program with a call to memcpy(). We can't guess where you added the
memcpy() call (especially since there are two different things called
"c" in your program) or what else you might have changed at the same
time. Show us real code. "Doctor, I've been having headaches. I'll
send my brother to see you; he doesn't have headaches, but I'm sure
you can diagnose my problem by examining him."

Finally, telling us "... and it doesn't help", um, doesn't help. I'm
sure there are a large number of things your program doesn't do.
Telling us what it *does* do is far more helpful. Don't tell us it
doesn't work; tell us exactly *how* it doesn't work.

If any of this sounds harsh, it's not intended that way. Asking good
questions isn't easy. <http://www.catb.org/~esr/faqs/smart-questions.html>
is a good guide.

And since you're posting through Google, please read
<http://cfaj.freeshell.org/google/>.
 
J

Joe Wright

Jordan said:
#include<stdio.h>
void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
}

int main()
{
char *c;
f(&c);
printf("%s\n", *c);
return 0;
}

I'm trying to get the printf to print hello, but I tried memcpy(*c,
"Hello", 6) and it doesn't help. Is my memory allocation correct?
Try this..

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

void f(char **c)
{
*c = malloc(6);
if (*c) strcpy(*c, "Hello");
}

int main(void)
{
char *c;
f(&c);
if (c) printf("%s\n", c);
return 0;
}

I'll leave the analysis to you.
 
J

Jack Klein

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

void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);

Completely undefined behavior, calling malloc() without a prototype in
scope. This is always undefined behavior unless the un-prototyped
function returns an int (which malloc() most certainly DOES NOT. Had
you not used the cast on the return, which is a very bad thing to do
in C, the compiler would have issued a diagnostic and (hopefully)
caused you to do the right thing.
memcpy (*c, "hello", 6);
}


int main()
{
char *c;
f(&c);
printf("%s\n", c);
return 0;
}

c is a pointer. You can't use *c in printf().

You most certainly can. For example, once you fix your undefined
behavior by including <stdlib.h> and removing the cast on the return
value from malloc(), you could quite properly code:

printf("%c\n", *c);
 
B

Barry Schwarz

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

void f(char **c)
{
*c=(char*)malloc(sizeof(char)*6);
memcpy (*c, "hello", 6);
}


int main()
{
char *c;
f(&c);
printf("%s\n", c);
return 0;
}

c is a pointer. You can't use *c in printf().

Sure you can. It will work with %d or %c BUT not with the %s that is
in the code.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top