Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//assigning one  string  to other string  using  pointer
int main(){
char ch[] = "Hello";
char pa[10];
char *ptr;
ptr=ch;
          //HERE WE WILL BE TAKING  WHILE LOOP (*ptr!='\0')

pa=ptr;
pa++;
ptr++

can  we  assign in these manner or else we  have to take  pa[some variable]=ptr; or *pa=*ptr;or pa=ptr;
and  while incrementing can we take pa++ or else pa+1;or pa[some  variable]++;
in last *pa='\0';or pa[some variable]='\0';end string

What I have tried:

i  have  tried and  got  output  but confuse
pa[some variable]=*ptr;
pa+1;
pa[some variable]='\0';
Posted
Updated 18-Sep-22 20:15pm
Comments
Richard MacCutchan 19-Sep-22 4:41am    
This is your third question on this subject (the first one back in May) and you still do not seem to understand the difference between an array (which is a fixed address) and a pointer which is a variable that can be modified.

Um. That's confused code!
You create two char arrays:
C
char ch[] = "Hello";
char pa[10];
And by the C language definition, the name of an array is a pointer to the first element, so both ch and pa are pointers to char values.
Then you create a char pointer and set it to one of them:
C
char *ptr;
ptr=ch;
But then you overwrite one of the array pointers with the other:
C
pa=ptr;
Which means that you no longer have anything which can access the second array data!

If you are trying to copy data from one array to the other using pointers, then you need to create two new pointers so that you can "walk" them through the arrays:
C
char inp[] = "Hello World!";
char out[13];
char *pinp = inp;
char *pout = out;
for (int i = 0; i < 13; i++)
   {
   *pout++ = *pinp++;
   }
printf("%s\n", out);
 
Share this answer
 
Comments
merano99 19-Sep-22 19:28pm    
Are you serious? i < 13; It would make more sense to check if pin points to 0 and index i is inside sizeof(out), right?
OriginalGriff 20-Sep-22 0:50am    
I absolutely agree.
But ... this is clearly an absolute beginner who has no idea what he is doing - let's get some code working first, so he stands a chance of understanding what pointers are and how they work before we start adding functions he hasn't met yet (and doesn't need because in the real world we would use a while loop testing for the null terminator!)
merano99 20-Sep-22 11:41am    
If you don't pay attention to the details when learning, you probably won't later. I would hope that the questioner would not use a while loop to copy a string in the real world, but perhaps rather strcpy_s(). But sure, if it's only enough to get some code running with effort, you have to compromise unfortunately .
Try
C
#include <stdio.h>

int main()
{
  char s[] = "Hello";
  char a[10];
  char *p;

  p = s; // here 'p' is just an alias of 's'

  printf("s = %p, p = %p\n", a, p);

  printf("content of s, pointed by p: '%s'\n", p);


  // now let's make a copy of 's' in 'a'
  int n;
  for (n=0; *p; ++n, ++p)
  {
    a[n] = *p;
  }
  a[n] = '\0';

  printf("content of a: '%s'\n", a);

  return 0;
}


Note, the C library provides the strcpy function, for the very task of copying a string:
C
#include <stdio.h>
#include <string.h>

int main()
{
  char s[] = "Hello";
  char a[10];

  strcpy(a,s);

  printf("content of a: '%s'\n", a);

  return 0;
}
 
Share this answer
 
Comments
merano99 19-Sep-22 19:32pm    
It would be good to query the limit of a[] as well.
CPallini 20-Sep-22 2:12am    
Definitely. Neither my code nor strcpy does it.
sundresjh 22-Sep-22 23:45pm    
//I will be learning look forward learn from mistake
for (n=0; *p; ++n, ++p)Here in condition what does *p means
{
a[n] = *p;
} how its working please can anyone explain
CPallini 23-Sep-22 1:52am    
The condition in place (*p) is:
"iterate until the character pointed by p is NOT the string terminator (i.e. zero)".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900