How to set a string array?

The question was asked: 8 years 1 months ago   views: 174

Tell me how in Win32 make an array that will store strings?


I need not immediately display a string, and after you enter the n-th number of strings and output those that match the condition, so I need somewhere to store!

Here is the code

#include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> int main() { LPTSTR mas[10]; for (int i = 0; i < 10; i++) { gets(mas[i]); printf("%s", mas[i]); } return 0; }

Writes all neobyatny ID.

Asked: 09-08-2011 в 21:14:53
I have a stream of characters, I need to display those rows that are greater than 80 characters. Here, in order to store them somewhere I need an array of strings. - 10-08-2011 в 04:33:57
Cleaned 2 neobyatny ID less.. - 10-08-2011 в 20:42:00
And if you think? Try to pick up. May have some thread of macros to define. Like UNICODE. And nice to give error messages in full. And, frankly, I'm not clear what vector did not like. - 10-08-2011 в 20:55:20
I write in Visual C++ 2010 and there are no operators like cout cin etc. so I don't know how to set the vector in it(( what you suggested maybe there is a solution to the problem, but how it will look in code, I have no idea.. - 10-08-2011 в 21:07:34
See VC++. To use cout and cin need to write std::cout and std::cin , respectively. Learn what namespaces. - 10-08-2011 в 21:12:58

Answers   2


"Thank you, will teach.. PS. read the book by B. Kernighan, D. Ritchie, the C programming Language."

Book is very good. Let's talk about the C (without pluses).

In my opinion, Your problem is that in C no built-in type "string". There is a built-in type character (char). Characters can be aggregated into arrays and thus be placed in adjacent (consecutive) bytes of memory. Declare this:

char string[1000]; // an array of 1000 bytes (characters)

A string in C call such an array, but rather the initial byte, terminated by zero (binary). When we write a constant string, for example

SAG *s = "string constant"; // this is also called string

the compiler allocates memory for these characters (creates an array of sufficient size), and enters its address in the variable s (of type pointer to character(s)).

In Your case an array of strings really needs to be an array of pointers to characters.

char *array[1000]; // here we will store the addresses of read rows (up to 1000) char buffer[1000]; // and here we read the next line int n = 0; // counter of read lines while (fgets(buffer,sizeof(buffer),stdin) { // why so, read man-pages array[n++] = strdup(buffer); // copy the read string into dynamic memory // and remember the address of the copy } // close standard input got here int i; for (i = 0; i < n; i++) if (strlen(array[i]) > 80) fputs (array[i],stdout); // again read the man puts and fputs

Overflow array[] too long lines, observe yourself.

If desired, the array can be made dynamic by declaring it char **array and independently placing it in memory, increasing as needed. To do this, read about realloc().

Answered: 10-08-2011 в 23:01:12
Good answer. Purely in C. - 10-08-2011 в 23:30:18
Thank you very much, tomorrow I will try. - 11-08-2011 в 04:18:28

Pure C++ (with STL) - std::vector<std::string> mas;

WinAPI - LPTSTR mas[]; // array of pointers to strings

MFC CStringArray mas;

Answered: 09-08-2011 в 22:57:38
Need some library to connect? Bo he gave me a bunch of errors.. - 09-08-2011 в 23:08:55
Yes. For the first #include #include For the second #include // Windows only For the third #include // and the dependence of MFC and Windows platform SDK - 09-08-2011 в 23:17:09
Pasib huge, there is ICQ? if you have more questions from a beginner?) - 10-08-2011 в 01:21:20
When connecting the #include library issued 173 error)) - 10-08-2011 в 01:26:50
try #include . The idea is all the same, these header files should cascade to connect. - 10-08-2011 в 02:05:22