We help IT Professionals succeed at work.

Arrays and pointers

Amulya
Amulya asked
on
I would like to do something like this
Char *ptr;
Char *ptr1;
Char ptr2[10];
Strcpy(ptr,?HELLO?);
When I do strcpy(ptr2,ptr) Now both ptr1 and ptr2 have ?HELLO?
How can I copy this to ptr2.strcpy will not work.

Is printf(?ptr1 is %s\n?,ptr1)  the correct way of printing ptr1.
Is using a for loop the only way to print ptr2
Thank you
Comment
Watch Question

Commented:
What you posted is correct. Both the strcpy and printf. You don't need to loop through single chars to print the string.
Don't forget to allocate memory for the

char *ptr;

before trying to copy some string to it. You have to do something like

ptr = new char[ strlen(ptr2) + 1 ];
strcpy( ptr, ptr2 );

In the case of

char ptr2[10];

10 bytes of memory are just allocated, and you can freely copy this amount of bytes to ptr2 (string length + 1 byte for '\0')

Author

Commented:
Thanks.

how do i use printf to print a char *ptr
 when i use printf("ptr is = %s",ptr) I get segmentation fault

 
Like ekc said:

Don't forget to allocate memory for the
char *ptr;

One alternative is to try
printf("ptr is = %s\0",ptr)

but this statement is dangerous.  It may yield unpredictable result in your program.

Commented:
What ekc says is right,
you are not clear about difference between a pointer and an array.
an array is an implicit constant pointer pointing to some memmory location,and an explicit pointer(char *ptr in your case) contain some junc address,so if you try to use ptr without allocating memmory then it may cause a memmory problem,
Read the book
"Understanding Pointer's in C"


  Here is your complete solution. We can say extension to ekc

#include<stdio.h>
#include<string.h>
#include<malloc.h>

void main()
{
char *ptr;
char *ptr1;
char ptr2[10];

ptr = (char*)malloc(strlen(ptr2)+1);

strcpy(ptr," Hello ");
strcpy(ptr2,ptr);

printf(" %s ",ptr);
}

Commented:
amulya,check you code you have totally three pointers,you wanted to print ptr1(but you never allocated memmory for it nor you have copied anyhting...its un altered)

From Your Code...

Char *ptr;
Char *ptr1;
Char ptr2[10];
>>Strcpy(ptr,?HELLO?);// Illegal
// so do something like this..
ptr = (char *)malloc(sizeof(char) * 6);
strcpy(ptr,"Hello");


>>When I do strcpy(ptr2,ptr) Now both ptr1 and ptr2 have ?HELLO?// how can ptr1 have "Hello";

i think you mistyped it should be,
strcpy(ptr1,ptr);//but still its illegal hence
//do something like this,
ptr1 = (char *)malloc(sizeof(ptr)+ 1);
strcpy(ptr1,ptr);
strcpy(ptr2,ptr);//will work fine as it already has memmory

Your Complete Program

Char *ptr;
Char *ptr1;
Char ptr2[10];
ptr = (char *)malloc(sizeof(char) * 6);
strcpy(ptr,"Hello");ptr1 = (char *)malloc(sizeof(ptr)+ 1);
strcpy(ptr1,ptr);
strcpy(ptr2,ptr);
printf("\nptr1=%s\nptr2=%s\nptr3=%s",ptr1,ptr2,ptr3);  

Commented:
where r u amulya
Thanks for returning here, and finalizing one open question.  BUT please update and finalize all the other open questions you have as well.  There are 13 of them.....
Some are current, many old.  If you need special handling help in any of them comment with details and I'll respond when I can.  The HELP DESK link on the left provides guidance in terms of Member Agreement, Guidelines and the Question/Answer processes.

If you award anything less than an "A" grade, please be sure to add comments as to why.

http://www.experts-exchange.com/jsp/qShow.jsp?ta=unix&qid=20108784
http://www.experts-exchange.com/jsp/qShow.jsp?ta=progsoftgen&qid=20107243
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=20164605
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20195786
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20263024
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20262125
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20261705
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259183
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259140
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20243128
http://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20201666
http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182968
http://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20154466

I will monitor them all and update each of them for you with this information so you can quickly and easily navigate through them, and to ensure that you receive Email notifications of these requests.

Expert input is always welcome if more is needed, and/or these remain open after one week so we can determine the fair outcome of the older items.

Thanks all,

Moondancer
Community Support Moderator @ Experts Exchange

Commented:
Moondancer

I will suggest to accept

     "ekc"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Thank you, Werner. Finalized today by Moondancer - EE Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.