Member Avatar for hwoarang69

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;

Member Avatar for miliardopiscrat

print that tab in this way:

printf("%s\n",test); 

or use std::cout

Member Avatar for Vish0203

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(); } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.