473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

tricky problem

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
8 1858


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


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

"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
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
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
#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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Lars Plessmann | last post by:
Problem: I try to store data in a objects field and read it out again. Sounds easy, yeah. But its a bit tricky here.... ;-) This is the class Customer.php with some setter and getter functions...
0
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode()...
4
by: Bung | last post by:
Hi, I have a tricky sql statment I have to write (tricky for me) and I am stuck. I'm having trouble with the following problem. Table1 (Column a, Column b, Column c) Table2 (Column a, Column...
25
by: PyPK | last post by:
What possible tricky areas/questions could be asked in Python based Technical Interviews?
5
by: Danny | last post by:
Hi there I need help with a tricky problem. I have a 2 dimensional array with qualities such as ball size, ball color, ball weight. Now I have to print out all the possible combinations of...
13
by: Steve Jorgensen | last post by:
== On Error Resume next, and Err.Number == If you want to call one of your procedures from another procedure, and check for errors afterward, you mayimagine that you should write code something...
2
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I...
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.