Array of String in C

Jan 17, 2021 at 8:14pm
Hello,

I'd like to create array of string in C. but I have Access violation error:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
	char* myString[100];
	int len = 3;
	char str[100];

	for (int i = 0; i < len; i++)
	{
		printf_s("String: ");
		gets(str);
		myString[i] = malloc(strlen(str) + 1);
		strcpy_s(myString[i], strlen(str) + 1, str);
	}

	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i - 1; j++)
		{
			if (myString[j] > myString[j + 1])
			{
				char *temp = myString[j];
				myString[j] = myString[j + 1];
				myString[j + 1] = temp;
			}
		}
	}

	for (int i = 0; i < len; i++)
	{
		puts(myString[i]);
	}


	return 0;
}
}
Last edited on Jan 17, 2021 at 8:44pm
Jan 17, 2021 at 8:27pm
Hello Shervan360,

So, where do you have this access violation? Or are you expecting someone to find it for you?

The access violation usually means that you went past the end of an array somewhere.

Andy
Jan 17, 2021 at 8:31pm
char* temp = myString[j];
this is weird. ^^^ what do YOU think it does?
take the * out of that

what are you trying to do?
mystring is an array of string. you are swapping the characters of string 1 and 2 and 3 around, assuming all 3 strings are valid and have at least 3 or whatever letters.
Last edited on Jan 17, 2021 at 8:37pm
Jan 17, 2021 at 8:45pm
Thank you, I'd like to sort the array of strings.
Jan 17, 2021 at 9:04pm
ok.
consider strcmp for the comparison of 2 strings, line 23. what you have is not right.
also I was wrong, you are not looking at individual letters, I misread it. you have the right idea to swap the pointers.

I don't see an access violation, but print out i and j in the inner for loop each time and watch to see if that is behind it. and maybe j+1 as well, though you can do it in your head when checking.
Last edited on Jan 17, 2021 at 9:06pm
Jan 17, 2021 at 9:24pm
Hello Shervan360,

After I managed to get the program to compile and run I started looking closer at the sort code.

It looks like you changed your original code. DO NOT do that. It makes if difficult for others to what is wrong and the following replies do not seem to match.

As I recall your original sort loops were:
1
2
3
4
for (int i = 0; i < len; i++)
{
	for (int j = 0; j < len - 1; i++)
	{

There is 1 reason that you are having a problem.

You have changed this, but I have not tried it yet.

I do believe that your sort is sorting pointers not strings. The if condition needs to be doing a "strcmp" of the 2 strings. the code if (myString[j] > myString[j + 1]) would work using a std::string in C++, but not in C.

Andy
Jan 18, 2021 at 12:29pm
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
	char* myString[100] = {};
	int len = 3;
	char str[100] = {};

	for (int i = 0; i < len; i++) {
		printf_s("String: ");
		gets_s(str, 99);
		myString[i] = (char*)malloc(strlen(str) + 1);
		memcpy(myString[i], str, strlen(str) + 1);
	}

	for (int i = 0; i < len; i++)
		for (int j = 0; j < len - i - 1; j++)
			if (strcmp(myString[j], myString[j + 1]) > 0) {
				char* temp = myString[j];

				myString[j] = myString[j + 1];
				myString[j + 1] = temp;
			}

	for (int i = 0; i < len; i++)
		puts(myString[i]);

	for (int i = 0; i < len; i++)
		free(myString[i]);

	return 0;
}


Don't forget to free the allocated memory!
Last edited on Jan 19, 2021 at 9:43am
Topic archived. No new replies allowed.