Link to home
Start Free TrialLog in
Avatar of Amulya
Amulya

asked on

Arrays and pointers

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
ASKER CERTIFIED SOLUTION
Avatar of ekc
ekc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amulya
Amulya

ASKER

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.

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

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);  
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.

https://www.experts-exchange.com/jsp/qShow.jsp?ta=unix&qid=20108784
https://www.experts-exchange.com/jsp/qShow.jsp?ta=progsoftgen&qid=20107243
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=20164605
https://www.experts-exchange.com/jsp/qShow.jsp?ta=cplusprog&qid=20195786
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20263024
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20262125
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20261705
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259183
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20259140
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20243128
https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=20201666
https://www.experts-exchange.com/jsp/qShow.jsp?ta=perl&qid=20182968
https://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
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