- #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..
5 1548
#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: -
strcpy(darr,"dest");
-
strcpy(sarr,"source");
-
"=" operator can be used with the strings only during the initialisation. I hope this helps you.
Regards,
Pradeep
- #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..
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
iam using fedora core linux...
yes now the problem is solved ....
thanks all ..
this program was executed in gcc, os is linux
yes now the problem is solved ....
thanks all ..
Glad to hear the problem was solved. We are here to please. ;)
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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)...
|
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...
|
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()
|
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...
|
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 *'...
|
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'...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
|
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...
| |