473,399 Members | 4,177 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,399 software developers and data experts.

arrays in c, and global variables

Hi, I am in the beginning phase of writing a stack in C. My problem is that when I try to initialize a stack, I want to be able to access the stack in other methods that I am going to write later. I was planning to creating a global variable and having that be the stack. However, when I try to do this, I get an "incomplete type" error. Also, in case its not clear, I am using a 2-d array and am having each element in the outer array be a string with a max length of SIZEW. Below is my code and any help is appreciated...


Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int currentItems;
  6. int maxSize;
  7. int SIZEW=100;
  8. char *contents[];   //this is where I get the error; I think it is because I don't have a number inside the brackets.
  9.                               // I don't have a number inside the brackets because the size isn't known until StackInitialize is called.
  10.  
  11. // Initialize an empty stack of a maximum of `size' elements.
  12. extern int StackInitialize(int size)
  13. {
  14.     int ii;
  15.     int jj;
  16.     maxSize = size;
  17.     char (*newContents)[SIZEW];    
  18.     newContents = malloc(size*SIZEW);
  19.  
  20.     if (newContents == NULL)
  21.     {
  22.         return 1;
  23.     }
  24.     for (jj=0; jj<size; jj++)
  25.     {
  26.         for (ii=0; ii<1; ii++)
  27.             newContents[jj][ii] = '\0';  //initialize the first character of each string element to the end-of-string character
  28.     }
  29.  
  30.     contents = newContents;    //assign newContents, which is local, to contents, which is global
  31.  
  32.     return 0;
  33. }
Oct 1 '08 #1
2 1699
oler1s
671 Expert 512MB
You cannot create an array of unknown size. You can have a pointer. And then later point it to a dynamically allocated array.
Oct 1 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
By any chance have you already written a linked list?

You can implement a stack by adding to the end of the list (the push) and removing from the end (the pop) and looking at the end (the peek).

Saves writing linked list code over again.
Oct 1 '08 #3

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

Similar topics

2
by: Mark Hannon | last post by:
I am trying to wrap my brain around storing form elements inside variables & arrays before I move on to a more complicated project. I created this simple example to experiment and as far as I can...
7
by: Kieran Simkin | last post by:
I have in my project in main.c a number of arrays defined like this (outside of any functions): char muser, mpass; I'm declaring them in a number of other .c files (inside functions) like this:...
197
by: Steve Kobes | last post by:
Is this legal? Must it print 4? int a = {{1, 2}, {3, 4}}, *b = a; printf("%d\n", *(b + 3)); --Steve
1
by: Roy Gourgi | last post by:
Hi, I am new to C#. I am trying to create some variables and arrays that can be seen throughtout the whole program. I have no choice as they have to be seen by the whole program. Where and how...
14
by: Stainless | last post by:
I have a public class Globals, which obviously holds all my global data. I have an array of 243 items, currently structs of type typedef struct STAR { int x; int y; int stellar_class;...
2
by: robin.bruce | last post by:
Hi guys, I just tried to give advice to a colleague who has a problem program in C, and I was interested to hear the thoughts of this forum on the subject. I've been an occasional programmer...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
1
by: cykhung | last post by:
I am using gcc to compile our C code under Linux. After compiling and linking, we use SWIG to create python wrapper for the C code so that we can execute C functions in python. My question is:...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
5
by: Chandra | last post by:
Hi, I have three files in which i am using 10 arrays each of size( say about 600 i.e A1,A2 ......A10). I am having some memory issues so i want to make use of the memory in a appropraite way....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
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...

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.