0

this is my code; you can see the errors in the bottom. I know that arrays in C are seen like pointers, but I'm pretty sure the declarations is right (char str). I don't know why it doesn't print anything

#include <stdio.h>
#include <stdlib.h>
#define N 3     
#define M 10

/* run this program using the console pauser or add your own getchsystem("pause") or input loop */


int main(int argc, char *argv[]) {

    srand(time());      //to generate each run different numbers
    char str [N][M];
    int i,j;
    i=0;

    str [0][0]="good";
    str[1][M]="morning";
    str[2][M]="world";

    for (j=0;j<N; j++) {
        i=rand()%(N-0+1)+1;     //formula to generate randoms numbers
        printf("%s",str[i]);    
    }

    return 0;
}
11
  • 1
    In C you can't use = to copy a string, please use strcpy. The = syntax can only be used for a string pointer, or when initialising an array in the statement where it is defined. You should enable all compiler warnings and act on them. "I know that arrays in C are seen like pointers" is a partial truth - when they are passed to a function. Commented Oct 22, 2019 at 16:49
  • 2
    Note too that i = rand() % N; is correct, not i = rand() % (N+1) + 1; Commented Oct 22, 2019 at 16:54
  • 1
    So one way would be to use strcpy(str[0], "good"); and so on. Commented Oct 22, 2019 at 16:56
  • 1
    also note that with str[x][y] you're accessing a single char. A string is a NUL-terminated array of chars. Locations str[0], str[1], and str[2] each contain M chars, so your strings can be max M-1 chars long each (the last char in the string must be 0). Commented Oct 22, 2019 at 16:56
  • 1
    Welcome to StackOverflow! Kudos for posting a minimal program. But you need to learn to do basic initial debugging. Please read ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Oct 22, 2019 at 17:56

2 Answers 2

1

First, turn on compilation warnings. You should get warnings because your code isn't valid. For example, with gcc I get:

randstr.c: In function ?main?:
randstr.c:16:15: warning: assignment makes integer from pointer without a cast [enabled by default]
     str [0][0]="good";
               ^
randstr.c:17:14: warning: assignment makes integer from pointer without a cast [enabled by default]
     str[1][M]="morning";
              ^
randstr.c:18:14: warning: assignment makes integer from pointer without a cast [enabled by default]
     str[2][M]="world";

This is because you are assigning character string pointers to char elements in the array, and each char element holds one character (generally, 8 bits.)

A simpler way to code this would be to use char *str[N] rather than char str[N][M].

But the reason your program crashes with segmentation fault is that time() takes an argument, and you're not passing one. Try time(NULL).

Here's how it would be coded using char *str[N]:

#include <stdio.h>
#include <stdlib.h>
#define N 3

int main(int argc, char *argv[]) {

    srand(time(NULL));      //to generate each run different numbers

    char *str[N];
    int i,j;

    str[0] = "good";
    str[1] = "morning";
    str[2] = "world";

    for (j=0; j<N; j++) {
        i = rand() % N;     //formula to generate randoms numbers
        printf("%s\n",str[i]);
    }

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

There's a few things wrong with your code here.

In C, you cannot assign strings using the assignment operator unless it's a string pointer or you're initializing. See here for the man page on strcpy.

Using the above method, you can define your strings like so:

    strcpy(str[0], "good");
    strcpy(str[1], "morning");
    strcpy(str[2], "world");

Your rand function call should also just be i = rand() % N;

When you were calling str[N][M] you were actually accessing a single character of the string rather than the whole thing. ex.

str[0][0] = 'g'
str[0][1] = 'o'
str[1][0] = 'm'
str[1][1] = 'o'

1 Comment

Thank you, I have understood that. That’s a pity It still doesn’t work... I have written str[N][M]={“good”,”morning”,”world”} and tried to primt the strings with (for example) printf(“%s”, str[1]). Is any if this wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.