473,326 Members | 2,090 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,326 software developers and data experts.

strcpy help

63
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>
  3. main() {
  4.   char* sptr,sarr[10];
  5.   char* dptr,darr[10];
  6.   darr[10]= "dest";
  7.   sarr[10]="source";
  8.   sptr=sarr;
  9.   dptr=darr;
  10.   printf("\n%s",darr);
  11.   strcpy(darr,sarr);
  12.   printf("\n%s",darr);
  13. }
the string is not copied from source to destination array, it prints some junk....help me where iam wrong..
May 11 '07 #1
5 1548
pradeep kaltari
102 Expert 100+
#include <stdio.h>
#include<string.h>
main() {
char* sptr,sarr[10];
char* dptr,darr[10];
darr[10]= "dest";
sarr[10]="source";
sptr=sarr;
dptr=darr;
printf("\n%s",darr);
strcpy(darr,sarr);
printf("\n%s",darr);
}

the string is not copied from source to destination array, it prints some junk....help me where iam wrong..
Hi askcq,
In line 6 and line 7 of your code, the method used to assign the strings "dest" and "source" to the arrays is incorrect. Try this:

Expand|Select|Wrap|Line Numbers
  1. strcpy(darr,"dest");
  2. strcpy(sarr,"source");
  3.  
"=" operator can be used with the strings only during the initialisation. I hope this helps you.

Regards,
Pradeep
May 11 '07 #2
AdrianH
1,251 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include<string.h>
  3. main() {
  4.   char* sptr,sarr[10];
  5.   char* dptr,darr[10];
  6.   darr[10]= "dest";
  7.   sarr[10]="source";
  8.   sptr=sarr;
  9.   dptr=darr;
  10.   printf("\n%s",darr);
  11.   strcpy(darr,sarr);
  12.   printf("\n%s",darr);
  13. }
the string is not copied from source to destination array, it prints some junk....help me where iam wrong..
Are you saying that your source array is printing out good stuff? I'm surprised that the compiler even allowed you to compile that.

The statement darr[10]= "dest"; is invalid. Use strcpy instead. That or assign it at declaration.

Could you please tell me what compiler you are using?


Adrian
May 11 '07 #3
askcq
63
iam using fedora core linux...
May 14 '07 #4
askcq
63
yes now the problem is solved ....
thanks all ..
this program was executed in gcc, os is linux
May 14 '07 #5
AdrianH
1,251 Expert 1GB
yes now the problem is solved ....
thanks all ..
Glad to hear the problem was solved. We are here to please. ;)


Adrian
May 14 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
23
by: JC | last post by:
hi, i want to combine two string together.. and put in to another string. how can i do . i try myself.. with the follow code. but seem can't get the result i want.. i want to get the result with...
9
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1)...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
4
by: a2x | last post by:
Hi, I've fixed this error, but I don't know why it occurs. Do you? Code: #include <stdlib.h> #include <string.h> void another(); int main()
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
16
by: mdh | last post by:
I have a question about the library function strcpy. In K&R on page 109, strcpy is used to copy a line ( line ) to a pointer (char *), which is first allocated space by K&Rs "alloc" function. In...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.