0

this code is a headstart to make a simple dictionary. im trying to insert the word and the meaning into array. what seems the problem?

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

main(){
    char alp, *a[5][1], item[100],item2[150];
    for (int i =1;i <=5 ; i++){
        printf("\n\nEnter item [%d][%d]:",i,0);
        scanf("%s",&item);
        printf("Enter item meaning [%d][%d]:",i,0);
        scanf("%s",&item2);
        strcpy(a[i][0],item); //program stops here
        strcpy(a[i][1],item2);
        printf("word %s \nmeaning: %s",a[i][0],a[i][1]);
    }
  }
Adam_Code
  • 25
  • 1
  • 10
  • 2
    Q: "What is `a[i][0]`?" A: "It is a pointer." Q: "Where does it point to?" A: "hmmmmmmmmmmm" – pmg May 13 '16 at 15:47
  • 1
    `scanf("%s",&item);` and `scanf("%s",&item2);` invoke *undefined behavior* by passing pointer to objects having wrong types: `%s` calls for `char*`, but what is passed are `char (*)[100]`. – MikeCAT May 13 '16 at 15:47
  • Using values of uninitialized variables having automatic storage duration `a[i][0]` and `[a][i][1]` also invoke *undefined behavior*. – MikeCAT May 13 '16 at 15:50
  • What are you asking? You haven't said what the problem is. (The title "Error in strcpy return not responding window" might be an attempt to describe the problem, but I can't figure out just what it means.) – Keith Thompson May 13 '16 at 16:15

3 Answers3

1

Your loop should be as follows:

for (int i =1;i <5 ; i++){

Notice the < rather than <=. If you declare an array of size 5, the last element is at index 4.

Also, initialize a as a[5][2][100] and not *a[5][1]

Harry
  • 1,362
  • 12
  • 19
1

Here you are

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

#define N   5

int main( void )
^^^^^^^^^^^^^^^^
{
    char a[N][2][150], item[150], item2[150];
         ^^^^^^^^^^^^

    for ( int i = 0; i < N ; i++ )
          ^^^^^^^^^^^^^^^^^^^^^^
    {
        printf( "\n\nEnter item [%d][%d]: ", i, 0 );
        scanf( "%s", item );
                     ^^^^
        printf( "Enter item meaning [%d][%d]: ", i, 1 );
        scanf( "%s", item2 );
                     ^^^^^
        strcpy( a[i][0], item );
        strcpy( a[i][1], item2 );

        printf("word %s \nmeaning: %s\n", a[i][0], a[i][1] );
    }
}

This declaration

char *a[5][1];

declares a multidimensional array of pointers. You need to reserve memory where to copy strings.

And indices of arrays start from 0.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • so ,did i just make mistake on the declaration of the char a[N][2][150], item[150], item2[150]; ? wew . thankyou this helps alot! – Adam_Code May 13 '16 at 15:59
  • @Adam_Code I underlined statements that are wrong in your code. For example you must to access elements of the array starting from 0. So for example the loop is also wrong. – Vlad from Moscow May 13 '16 at 16:01
  • thank you. the underline helps a lot for my understanding using the array. – Adam_Code May 13 '16 at 16:04
0

You haven't initialized the values in the a array, so you can't use them. When you call strcpy, you pass it the value of a[0][0] as the address to copy data into -- but it has no particular values yet because you've not set it to any value. So you're asking strcpy to copy the string to no place in particular.

You must assign a variable a value before you attempt to use its value. You must make a pointer point to something before you try to use the pointer's value as if it pointed to something.

You could do this:

a[i][0] = strdup (item);

The strdup function allocates memory and copies a string into that memory, returning a pointer to it. After this, a[i][0] will point to that newly-allocated memory that contains a copy of the string.

If your platform doesn't provide strdup, you can easily code it:

char *strdup (const char * i)
{
    char *j = malloc (strlen(i) + 1);
    if (j == NULL)
        return NULL;

    strcpy (j, i);
    return j;
}

You also access the array past the end. Your array has only five objects with indexes 0, 1, 2, 3, and 4. So using an index of 5 is also illegal.

Lastly, in your scanf calls, you pass the address of your arrays of characters, like this: scanf("%s",&item);. Since item is an array, it will decay into the address of its first element, which is what you want. You don't want the address of the array, so the & is incorrect.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278