I was trying to input an array of strings using pointers but failed. Here is the code:

int i=0;
char *p[6];       //array of strings
for(i=0;i<5;i++)
{
gets(p[i]);       // input string
}
for (i=0;p[i];i++){
printf(p[i]);          // print string
printf("\n");
}

The problem is that each time the first for loop executes, i input string from console, but p shows to contain NULL only. It does not contain the strings that I enter from console. The second loop does not execute, simply jumps out of the loop at first iteration since p[0] (and p[1], p[2] so on may be) contains NUll. Where is the problem? How can I correct it? Can anyone help me?

Recommended Answers

All 4 Replies

Did you allocated the memory? Try something like below.

#include <stdio.h>
#include <stdlib.h>

#define STR_SIZE 35/*don't use magic number use descriptive labels*/
#define ARR_SIZE 6

int main()
{
  int i = 0;
  char *p[ARR_SIZE];

  for (i = 0; i < ARR_SIZE; ++i)
	p[i] = (char*)malloc(STR_SIZE);/*allocate memory*/
  
  for(i = 0; i < ARR_SIZE; ++i)
  {
	fputs("Enter a string->", stdout);
	fgets(p[i], STR_SIZE, stdin);/*never use gets use fgets, see comments below*/
  }
  for (i = 0; i < ARR_SIZE; ++i)
  {
	printf(p[i]);
  }
  
  for (i = 0; i < ARR_SIZE; ++i)
	free(p[i]);/*free memory*/
  return 0;
}

/*
Never use gets().  Because it is impossible to tell without knowing the
data  in  advance  how  many  characters  gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is  extremely  dangerous  to  use.   It has been used to break computer
security.  Use fgets() instead.
*/

Also, when you post code please make sure its format correctly.

Thank you very much gerard4143. I tried to allocate memory, but erroneously. So it did not work. Then I observed that if I initialize the array it does not require memory allocation. Why does it happen? I have another question. I've written another code:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define row_size 3
#define col_size 2
#define str_size 10

void main(){

clrscr();

char *p[row_size][col_size];  //table of strings
int i =0, j=0;

  for (i=0; i<row_size; ++i)
 	for(j=0; j<col_size; ++j)
   	{
   	 p[i][j]=(char *)malloc(str_size);  //memory allocation
   	 fputs("enter a string: ", stdout);
   	 fgets(p[i][j],str_size,stdin);
   	}

  for (i=0; i<row_size; ++i)
  	for(j=0; p[i][j]; ++j)
   	
	   printf(p[i][j]);
	

  for (i=0; i<row_size; ++i)
  	 for(j=0; j<col_size; ++j)
   	free(p[i][j]);
   
getch();
   }

It outputs 12 strings, though I input 6 strings. The input were:

aa
bb
cc
dd
ee
ff

the output came:
aa
bb
cc
dd
ee
ff
cc
dd
ee
ff
ee
ff

But if I change the loop from

for(j=0; p[i][j]; ++j)

to

for(j=0; j<col_size; ++j)

the program works fine. And if I declare the array like this:

char **p;

Then how should I allocate and deallocate memory?

Thank you gerard4143. The link helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.