473,827 Members | 2,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char pointer array problem

6 New Member
Hi,

I have this code I wrote to copy any length lines into array of pointers. The problem is after storing the individual lines into the char pointer array, the dispaly() chops off some lines while retains the others (mostly last and first line in the array). I guess there is some allocation problem but don't know where exactly.

Please suggest.

Expand|Select|Wrap|Line Numbers
  1. int readlines(char *lineptr[], int maxlines)
  2. {
  3.         int len, nlines,i;
  4.         char *p, line[MAXLEN];
  5.  
  6.         len=0;
  7.         nlines=0;
  8.  
  9.         while( (len=getline(line,MAXLEN)) > 0)
  10.         {
  11.                 printf("len=%d\n",len);
  12.                 if(nlines >= maxlines)
  13.                         return -1;
  14.                 else if( (p=(char *) malloc(sizeof(len+1))) == NULL )
  15.                         return -1;
  16.                 else
  17.                 {
  18.                         line[len]='\0';
  19.                         strcpy(p,line);
  20.                         //lineptr[nlines]=(char *)malloc(sizeof(len));
  21.                         lineptr[nlines]=p;
  22.                         nlines++;
  23.                 }
  24.                 //free(p);
  25.         }
  26.  
  27.         printf("printing values of pointer lines in readlines() \n");
  28.         for(i=0;i<nlines;i++)
  29.     {
  30.                 printf(lineptr[i]);
  31.                 printf("\n");
  32.         }
  33.         //free(p);
  34.         return nlines;
  35. }
The single line comment in the code is what I'm not sure if I should do tht.
Thank you.
Nov 2 '06 #1
1 2168
Banfa
9,065 Recognized Expert Moderator Expert
You do not need either of the commented out lines, the problem is with this line

else if( (p=(char *) malloc(sizeof(l en+1))) == NULL )

You are allocating sizeof(len+1) bytes, len is an int and so in len+1 so you are allocating sizeof(int) bytes (typically 2 or 4).
Nov 2 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3474
by: techie | last post by:
I'm creating a class BookType that will store information about books. Each book type can have up to 4 authors. I defined a new type for an array of char: typedef char array4_t; This will hold 25 characaters I declared a 2 dimensional array of char in BookType:
2
3468
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip char* cstr; std::cout << "House/Appartment # "; std::cin.getline(cstr, CHAR_MAX); //<<==seg fault
5
3991
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a variable **var make it an array of pointers? I realize that 'char **var' is a pointer to a pointer of type char (I hope). And I realize that with var, var is actually a memory address (or at
12
5543
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the following program: #include<stdio.h> #define SIZE 1 int main()
15
1832
by: RAB | last post by:
I am programming in the palm environment using C++ and am stuck. I have created an char array: const char MyArray = { {"string1"}, {"string2"}, .... {"string50"} }
21
2310
by: Wisdo | last post by:
Hi All, char to char ** is compile error. why? if i hava a string array. yes. it's not safe and it's better to use vector<stringinstead. but my point is the language feature. char sex = {"Male", "Female" };
18
4072
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
3
3497
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/ #include <cstdio> #define MAX_WORD_LEN 80 #define MAX_SIZE 1000
30
3295
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
21
2763
by: arnuld | last post by:
int main() { const char* arr = {"bjarne", "stroustrup", "c++"}; char* parr = &arr; } this gives an error: $ g++ test.cpp test.cpp: In function 'int main()': test.cpp:4: error: cannot convert 'const char* (*)' to 'char*' in
0
10464
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10183
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7730
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6935
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5768
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4404
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3947
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.