-3

I can't figure out what is wrong with this program. I have tried using

                      strncpy(text,array[ ],sizeof(text)) 

already but that didn't solve anything. What I need is a simple method of copying string like in pascal language, where a simple equal sign can be used to copy string (or a generated function that can do this). this is the source code:

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

int x,y;

char array[10][10];
int choice;
char text[5];


int main()
 {
    for(x=0;x<5;x++)
     {
        printf("ENTER text: ");
        scanf("%s", text);   
        strcpy (text,array[x]);
     }

     for (y=0;y<5;y++)
      {
        printf("\n");
        printf("%s", array[y]);
      }
    return 0;
 }

the output should be something like;

"*string*"
"*string*"
"*string*"
"*string*"
"*string*"

but all i get is five spaces, no string. Any Solutions?

otboss
  • 621
  • 1
  • 7
  • 16
  • `strcpy (text,array[x]);` --> `strcpy (array[x], text);` – BLUEPIXY Feb 04 '16 at 14:52
  • it's `strcpy(destination, source)`, you're doing it backwards, so you're copying an empty array into `text` – Marc B Feb 04 '16 at 14:54
  • ok simple mistake, thanks but the real question is is any alternative to strcpy? – otboss Feb 04 '16 at 14:57
  • *"the real question is is any alternative to strcpy"* isn't that an X-Y question? The real problem is that you were not using `strcpy` correctly. Why re-invent library functions that work perfectly well (once you know how)? – Weather Vane Feb 04 '16 at 15:29
  • I am curious why one thinks `"*string*"` will fit in a `char text[5];` when using `scanf("%s", text);`? – chux - Reinstate Monica Feb 04 '16 at 16:01
  • there were some some cases i have encountered before where strcmp wasn't working correctly, the program crashes so i was wondering if there was any alternative to it @Vane – otboss Feb 04 '16 at 16:25
  • the asterisks and quotes aren't included. @chux – otboss Feb 04 '16 at 16:26
  • 1) `string` will not fit in `char text[5];`: no room for the null character. 2) since asterisks and quotes aren't included, posting them as part of expected output confuses this post's goal. 3) So what is the input tried - exactly? – chux - Reinstate Monica Feb 04 '16 at 19:04
  • the string shown is only a place holder for the actual string which has a strlen of < 6 and input would be something random like pp,nn,mm,ll,aa, as long as it shows in the order that is shown in the coding and yea ur right about the asterisk confusing the post – otboss Feb 04 '16 at 19:13
  • In an 5 element long char array, you can only fit strings which have an strlen() result less or equal to 4 (don't forget the terminating null character!). It is not strcmp() etc. not working correctly, rather a characteristic of the C language. It is not possible to figure out the length of a storage a pointer is pointing at (and those functions only "see" the pointers), so you have to add any necessary protection against such yourself. – Jubatian Feb 04 '16 at 20:06

3 Answers3

5

This statement

strcpy (text,array[x]);

is invalid. I think you mean

strcpy( array[x], text );

Take into account that you should write

scanf("%4s", text); 
       ^^^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • ok thanks for the fix but do you know of any alternative to strcpy? – otboss Feb 04 '16 at 14:54
  • @Questionare232 No, I do not know. Of course you can use such string functions like strncpy or memcpy but they differ from strcpy in the used logic. In C arrays do not have the assignment operator. You have to copy arrays element by element. – Vlad from Moscow Feb 04 '16 at 14:55
  • @Questionare232 You can enclose an array in a structure and in this case you can assign one object of the structure to another object in fact assigning the enclosed arrays. – Vlad from Moscow Feb 04 '16 at 14:58
1
strcpy (text,array[x]);       

Instead of this ,you should try this -

strcpy (array[x],text);

First argument of strcpy is the destination and second is source.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
1

You are passing parameters to strcpy in the wrong order.

See http://linux.die.net/man/3/strcpy:

char *strcpy(char *dest, const char *src);

dest means destination, src means source.

SirDarius
  • 41,440
  • 8
  • 86
  • 100