473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dynamic pointer array to strings

Ok I need to create a dynamic array of pointers to strings, the number of strings is determined by the typed value at the keyboard, and then the length of each string is determined as it's typed in
Expand|Select|Wrap|Line Numbers
  1. char **zerg;
  2. char ch;
  3.  int num;
  4.  int i;
  5.  char buffer[BUFSIZ];
  6. printf("Enter the number of names you wish to store>");
  7.  scanf("%d",&num);
  8. zerg = (char **)malloc(num);
  9. for(i = 0; i < num; i++)
  10.  {
  11.   fgets(buffer,BUFSIZ,stdin); 
  12.  if (buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)-1] = '\0';
  13.  
  14.  zerg[i] = malloc(sizeof(char)*(strlen(buffer)+1));
  15.  strcpy(zerg[i],buffer);
  16.  
I think I've referenced correctly using the **char but when I run the program I do not get the desired results from the output or for the memory address anyone able to help?
Oct 19 '09 #1
6 3856
newb16
687 512MB
You should
malloc(num*sizeof(char*));
because malloc accepts number of bytes to allocate.
Oct 20 '09 #2
Banfa
9,065 Expert Mod 8TB
In C you should cast the output of malloc

zerg = (char **)malloc(num);

should be

zerg = malloc(num);

In the C and C++ languages sizeof(char) is by definition 1 always.
Oct 20 '09 #3
newb16
687 512MB
@Banfa
Actually argument for malloc should be
malloc(num*sizeof(char*)),
because size of pointer is not 1.
Oct 20 '09 #4
Banfa
9,065 Expert Mod 8TB
@newb16
Actually my whole post was wrong because I wrote "should" instead of "shouldn't" :D

So stating again and recapping, when using malloc in C you should
  1. Make sure you allocate the right amount of data taking into account the size of the objects you are allocating and the number of objects you are allocating.
  2. Not cast the output of malloc which by passes C's (admittedly not brilliant) pointer conversions.
Oct 20 '09 #5
Ok so I've got the program running as I wanted, except it automatically allocates 16 bytes for each string even if the length is less than that. so for example turkey still gets allocated 16 instead of just 7 bytes. It does however expand correctly for longer strings.
Richard starts at address 00331A18
Gilmore starts at address 00331A28
Paul starts at address 00331A38
How can I get gilmore to start at say 00331A21 straight after the terminating character of richard.
Oct 20 '09 #6
Banfa
9,065 Expert Mod 8TB
You should understand that when you call malloc all you provide is a minimum size. That is the returned pointer to a pointer to a block of data of at least that many bytes.

However compiler/platform actually implements the memory allocation is implementation defined as long as it meets that requirement. Many implementations will allocate additional data for organisational or book keeping purposes. Many implementations have a minimum block size, any request for the blocks below the minimum size returns a block of the minimum size.

Finally malloc has to return a block that meets the alignment requirements of the most stringent structure that could be put into memory block. That is for instance it can't return a byte aligned block since words require word alignment.

If you have some additional requirement of how your data items are aligned with respect to each other malloc will not do it for you. You have to do it for yourself so you would need to allocate a block of memory large enough to hold all the strings and pack them into that data block yourself (obviously holding pointers to the start of each string).

If on the other hand you are just worrying about the the wasted space of the unused bytes don't. Just accept it as a fact of the implementation you are using.
Oct 20 '09 #7

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

Similar topics

5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
12
by: googlinggoogler | last post by:
Hi, Im new to C++ and trying to self teach myself whilst I sit at my unentertaining day job (thought i'd put that across before im accused of cheating on my homework, im 45...) Anyway I'm...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
blackstormdragon
by: blackstormdragon | last post by:
It seems pointers and dynamic arrays are giving me a hard time. Heres part of the assignment. We have to create a class named Student that has three member variables. One of the variables is called...
6
by: Paul | last post by:
Hi all, I have a string which I want to break into a char array, is the following way of doing this ok: const char* test = new char; test = sQuery.c_str(); and finally:
17
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.