By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,612 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,612 IT Pros & Developers. It's quick & easy.

tricky problem

P: n/a
Hi , below given question was asked to me during an interview and i
figured it out little tricky . It would be a great help if anyone
could solve it.

Code : -
main()
{
char *s1="abcd",*s2=NULL;

/* From here you call a function copy which has return type void .
Simple task is to copy s1 into s2 . */

copy(&s1,&s2);

}

copy(char **s,char **t)
{

----- NORMAL COPY STATEMENTS --------
/* But here we have to malloc " t " compulsory . Now what changes
should i do so that chage will reflect in main .I have to print in main
only. */

}

Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a


pr*********@gmail.com wrote:
Hi , below given question was asked to me during an interview and i
figured it out little tricky . It would be a great help if anyone
could solve it.

Code : -
main()
{
char *s1="abcd",*s2=NULL;

/* From here you call a function copy which has return type void .
Simple task is to copy s1 into s2 . */

copy(&s1,&s2);

}


Seems like trick question, how about a shallow copy?

void
copy(char **s,char **t)
{
*t = *s;
}

Don't forgot a function prototype for 'copy' and the correct
declaration of 'main'.

-Charlie

Nov 15 '05 #2

P: n/a


pr*********@gmail.com wrote:
Hi , below given question was asked to me during an interview and i
figured it out little tricky . It would be a great help if anyone
could solve it.

Code : -
main()
{
char *s1="abcd",*s2=NULL;

/* From here you call a function copy which has return type void .
Simple task is to copy s1 into s2 . */

copy(&s1,&s2);

}

copy(char **s,char **t)
{

----- NORMAL COPY STATEMENTS --------
/* But here we have to malloc " t " compulsory . Now what changes
should i do so that chage will reflect in main .I have to print in main
only. */

}


you may write the copy function as follows:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
copy( char **s, char **t)
{
size_t len ;
len = strlen(*s);

*t = malloc(len + 1);
strcpy(*t,*s);
return(0);
}

Also, you don't need to pass the address of s1. Passing s1 is
sufficient.
If you pass s1 to copy, then you can use following function.

int
copy ( char *s, char **t)
{
size_t len ;
len = strlen(s);
*t = malloc(len + 1);
strcpy(*t,s);
return(0);
}

Also, string literals may be placed in read only memory and so won't
be modified. In that case you don't even need to allocate the
space for s2.

int
copy(char *s,char **t)
{
*t = s;
return(0);
}

Nov 15 '05 #3

P: n/a
main()
{

char *s1="abcd",*s2=NULL;

copy(&s1,&s2);

printf("%s\n",s2);
}

void copy(char **s, char **t)
{

*t=(char *)malloc(4);
strcpy(*t,*s);
}


<pr*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi , below given question was asked to me during an interview and i
figured it out little tricky . It would be a great help if anyone
could solve it.

Code : -
main()
{
char *s1="abcd",*s2=NULL;

/* From here you call a function copy which has return type void .
Simple task is to copy s1 into s2 . */

copy(&s1,&s2);

}

copy(char **s,char **t)
{

----- NORMAL COPY STATEMENTS --------
/* But here we have to malloc " t " compulsory . Now what changes
should i do so that chage will reflect in main .I have to print in main
only. */

}

Nov 15 '05 #4

P: n/a

"Hash" <a@b.com> wrote in message
news:A2*****************@news.cpqcorp.net...
main()
{

char *s1="abcd",*s2=NULL;

copy(&s1,&s2);

printf("%s\n",s2);
}

void copy(char **s, char **t)
{

*t=(char *)malloc(4);
strcpy(*t,*s);
and now you have just caused a memory access problem, "abcd" is 5 chars
long - remember the \0 at the end ;-)
Allan
}

Nov 15 '05 #5

P: n/a
Hash top-posted (BAD Hash, no biscuit):
main()
{

char *s1="abcd",*s2=NULL;

copy(&s1,&s2);

printf("%s\n",s2);
}

void copy(char **s, char **t)
{

*t=(char *)malloc(4);
strcpy(*t,*s);
}


BOOM.

(a) dangerous cast of `malloc`.

(b) no `#include <stdlib.h>`.

(c) no check for malloc returning null.

(d) Copying 5 characters (`a`, `b`, `c`, `d`, 0) into
a mallocation with room only for 4.

--
Chris "electric hedgehog" Dollin
It's called *extreme* programming, not *stupid* programming.
Nov 15 '05 #6

P: n/a
Hash wrote:

Rude top posting fixed. Your reply belongs *under* the text you are
replying to.
<pr*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hi , below given question was asked to me during an interview and i
figured it out little tricky . It would be a great help if anyone
could solve it.
If you are ready for a C programming job then, IMHO, this is not tricky.
Code : -
main()
{
char *s1="abcd",*s2=NULL;

/* From here you call a function copy which has return type void .
Simple task is to copy s1 into s2 . */

copy(&s1,&s2);

}

copy(char **s,char **t)
{

----- NORMAL COPY STATEMENTS --------
/* But here we have to malloc " t " compulsory . Now what changes
should i do so that chage will reflect in main .I have to print in main
only. */

}

Include the appropriate headers for the library functions you are using,
otherwise bad things can (and sometimes do) happen.

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

Since your copy function is defined after it is used, provide a
prototype declaration to ensure everything ties together nicely.

void copy(char **s, char **t);
main()
main returns an int, tell the compiler this. Especially as in C99
implicit int is no longer allowed.

int main(void)
{

char *s1="abcd",*s2=NULL;

copy(&s1,&s2);

printf("%s\n",s2);
}

void copy(char **s, char **t)
{

*t=(char *)malloc(4);
The return value of malloc is a void* so the cast is not required. If
the compiler complains without the cast then either you have not
included stdlib.h or you are compiling as C++.
strcpy(*t,*s);
Bang. You need to allow space for the '\0' termination of the string as
well. Also you are not checking for malloc failing.

size_t len = strlen(*s) + 1; /* +1 for the null termination */
*t = malloc( len );
if (*t != NULL)
memcpy(*t, *s, len); /* might as well use memcpy as we know

the length already. */

/* Note that on malloc failure the destination is set NULL so
the caller needs to check this before using the value. */
}

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #7

P: n/a
#include <stdio.h>
#include <stdlib.h>

copy(char **, char **);

main() {
char *s1="abcd", *s2=NULL;

copy(&s1, &s2);

/*
* test it ;)
*/
printf("%s\n", s2);
}

copy(char **s, char **t) {
*t=malloc(strlen(*s)+1);
if(*t!=NULL)
strcpy(*t, *s);
}

Nov 15 '05 #8

P: n/a
ma******@gmail.com wrote:
#include <stdio.h>
#include <stdlib.h>

copy(char **, char **);

main() {
char *s1="abcd", *s2=NULL;

copy(&s1, &s2);

/*
* test it ;)
*/
printf("%s\n", s2);
}

copy(char **s, char **t) {
*t=malloc(strlen(*s)+1);
if(*t!=NULL)
strcpy(*t, *s);
}


Did you try to compile this program with e.g. gcc -Wall -Wextra ??
Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.