tricky problem

P

pras.vaidya

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. */

}
 
C

Charles Mills

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
 
J

junky_fellow

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);
}
 
H

Hash

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);
}
 
A

Allan Bruce

Hash said:
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
 
C

Chris Dollin

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.
 
F

Flash Gordon

Hash wrote:

Rude top posting fixed. Your reply belongs *under* the text you are
replying to.

If you are ready for a C programming job then, IMHO, this is not tricky.

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 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. */
 
M

martinaw

#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);
}
 
G

Grumble

#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 ??
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top