0

trying to copy pointer in array.
lets say *pointer has the value "COMPUTER". and i want to store in array[o];
note: i want to use strcat to do this and i do not want to do some thing like "array[0]"

char test[10];
char *point;         //has the value "COMPUTER"

for(int i = 0; i < 10; i++)
{
  test[i] = '\0';
}

strcat(test, point);      //store value at end of test array. so test[0] is computer

for(int i = 0; i < 10; i++)
{
  printf("%s",test[i]);
}

not sure why this dont work;

3
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by Vish0203
0

1st of all there is "Multiple declaration of "i" " instead of declaring i twice in for loop, declare it once like

int i; 

between the lines 1 and 2.
=> test[0] cant be computer, "Computer" is a string and test[0] is a character type.
=> there is no use of second for loop. and even if you are using, in printf statement write,

printf("%c",test[i]);

because, as i mentioned in 1st point, test[0], test[1],........test[10] are characters.. only "test" is string.

you can use the following code:

#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
 {
  char test[10];  int i;
char *point="COMPUTER";
for(i=0;i<10;i++)
{
 test[i]='\0';
}
strcat(test,point);
printf("%s",test);
getch();
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.