C programming

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Hi there,

I know that Java normally has got .startswith to check if it from the start

but for C i use strcmp is it correct?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Hi there,

I know that Java normally has got .startswith to check if it from the start

but for C i use strcmp is it correct?

You use strncmp in this case, the "n" number of characters to compare should be equal to the length of the literal string you are comparing against.

If not, what you are effectively comparing is the exact of the 2 strings, not just the prefix.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Hi there,

I am trying to learn c programming,it was mentioned that C is backward compatiable with java right?

So far I have explored options and found out that visual C++ 2010 is hard to use compared to borland commandline tools.But I was ask to used visual studio because I was developing an assignment in openGL,there are some linker errors...like no incremental..cannot find .exe files etcs...takes me weeks to figure this out..cannot import glut.h etc

What is the equivalent of ArrayList or array implementation in C?
In java there is strtokenizer,but for c it is like strtok which is a pointer.

even declaring a string in c needs to use like char str[] or char str*

I need to cast a string to float needs atof,but for just I just use Integer.parseInt():

event printing a line needs use printf()..where in python just print() or java just System.out.println()

I am trying to write a parser btw.

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

struct vertex {
int vid;
float x;
float y;
float z;
float nx;
float ny;
float nz;
};

struct face{
int fid;
float v1;
float v2;
float v3;
};

int main(void) {
char filename1[80];
FILE *fp1;
printf("Enter the input file name:");
gets(filename1);
fp1=fopen(filename1,"r");
printf("EOF is %d",EOF);
char string[100];
if (fp1!=NULL)
{
char *ch;
while( fgets(string,100,fp1) != NULL)
{
if(strcmp(string,"Vertex") == 1){
printf("\n");
do{
if(strcmp(string,"Vertex") == 1){
struct vertex m;
int c = 0;
/* printf("%s",string); */
ch = strtok(string, " ");
while(ch != NULL){
char *type = "";
if(c == 0){
type = ch;
}else if(c == 1){
m.vid = atof(ch);
}else if(c == 2){
m.x = atof(ch);
}else if(c == 3){
m.y = atof(ch);
}else if(c == 4){
m.z = atof(ch);
}
float len = sqrt(pow(m.x,2)+pow(m.y,2)+pow(m.z,2));
m.nx = m.x/len;
m.ny = m.y/len;
m.nz = m.z/len;
c++;
ch = strtok(NULL, " ");
}
printf("%i %f %f %f =normal(%f %f %f)\n",m.vid,m.x,m.y,m.z,m.nx,m.ny,m.nz);
}
else if(strcmp(string,"Face") == 1){
struct face m;
int c = 0;
/* printf("%s",string); */
ch = strtok(string, " ");
while(ch != NULL){
char *type = "";
if(c == 0){
type = ch;
}else if(c == 1){
m.fid = atof(ch);
}else if(c == 2){
m.v1 = atof(ch);
}else if(c == 3){
m.v2 = atof(ch);
}else if(c == 4){
m.v3 = atof(ch);
}
c++;
ch = strtok(NULL, " ");
}
printf("%i %f %f %f\n",m.fid,m.v1,m.v2,m.v3);
}
}while( fgets(string,100,fp1) != NULL);
}
}
}
fclose (fp1);
return(0);
}
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
Hi there,

I am trying to learn c programming,it was mentioned that C is backward compatiable with java right?

So far I have explored options and found out that visual C++ 2010 is hard to use compared to borland commandline tools.But I was ask to used visual studio because I was developing an assignment in openGL,there are some linker errors...like no incremental..cannot find .exe files etcs...takes me weeks to figure this out..cannot import glut.h etc

What is the equivalent of ArrayList or array implementation in C?
In java there is strtokenizer,but for c it is like strtok which is a pointer.

even declaring a string in c needs to use like char str[] or char str*

I need to cast a string to float needs atof,but for just I just use Integer.parseInt():

event printing a line needs use printf()..where in python just print() or java just System.out.println()

I am trying to write a parser btw.

I think you are greatly mistaken. Java is nothing of the sort of similar language to C. C is a 2nd generation modular language, while Java is a 3rd generation Object oriented language of much more complicated construct. Both operate with different mode of encapsulation and segregation in software design methodology.

All IDE have learning curve that you need to get yourself acquaintance with. The complexity they introduce in return offer faster configuration of complicated compilation setup, more features in code searching and assistance. So you might wanna read up some books on Visual Studio C++ and see if it helps in your process.

I don't see how you can find so called equivalence of ArrayList in Java and Array in C. Array in C is a very simple construct. Just a block of contiguous memory allocated either static in the data segment, or you can create a block of memory in the heap using malloc, and affix your type over it using any array pointers.

ArrayList on the other hand is a much more complicated construct in Java. It is a class with methods, the language has a very strict control over how a class is created, used with the methods it provides and so forth. It can be implemented in C, but it is definitely a lot more than just a simple Array in C.

It's confusing in your question, are you looking for help in Java or C ?

Java has no pointer, pointer are very raw memory address manipulator construct in C. Java references are higher form of pointers where the JVM perform strict checking on their typing and where they point to and how they interact with the garbage collector.

In C, to declare a string, or characters of array, in either the data or stack segment
Code:
char arr[5];

In C, dynamic array allocation is possible using malloc and its cousin functions such as calloc etc.
Code:
char *arr = malloc(sizeof(char) * 5);
char arr[] is just another denotation of pointers because arrays in C are very much interchangeable between pointers and itself.

You don't cast a string, you parse it. Casting a string is still a string. "atof" is a conversion function, similar to Integer.parseInt method.

I don't think I need to go further, except to tell you that Java and C are miles apart.

If you need to write a parser in C, try using Bison, or Yacc. If you need one in Java, try reading on JavaCC.

Parsing is a pretty complicated process. Unless you are task to understand better on how parsing works, then feel free to handcode yourself, if not, there are libraries out there to do this for you.

Google and spend some time to find out on how lexical analysis and parsing are done.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
for my parser I am trying to do IO of a file in C.

the file I am trying to read into struct is:
Vertex 1 0.120846 0.0669721 -0.0628787
Vertex 2 0.312241 0.00198185 0.0681098
Vertex 3 0.140731 0.444648 -0.0623968
Face 1 16563 41411 41412
Face 2 16567 16597 22256
Face 3 22245 16567 22256

say I don't know how many lines to read,i want to know know to insert into that dynamic array you mention.

I am quite sure there is way to

//save vertex struct into vertex array

cause my assignment got these but still trying to find out,i saw you code it is char *arr = malloc(sizeof(char) * 5);

I need a large size say about 10k lines

btw there is EOF -1 on my code,and somemore it skips to line 2 straight away,on my teachers code there is somewhere along the line that says type! =EOF && type !='\n'
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
for my parser I am trying to do IO of a file in C.

the file I am trying to read into struct is:



say I don't know how many lines to read,i want to know know to insert into that dynamic array you mention.

Okay, consuming input have quite a number of ways, you will need to think about how to consume input in a more robust way in the future.

I will give you something simple for you to consider

From the perspective of C, a file is just a series of bytes, there is no notion of multiple lines, that's what you perceive when the display print in multiple lines due to the CRLF limiters.

One way to look at your input is as follows

Code:
Vertex 1 0.120846 0.0669721 -0.0628787 Vertex 2 0.312241 0.00198185 0.0681098 Vertex 3 0.140731 0.444648 -0.0623968 Face 1 16563 41411 41412 Face 2 16567 16597 22256 Face 3 22245 16567 22256

All characters under this group are consider whitespaces '\t', ' ', '\r', '\n', '\f', '\v'

So actually your parsing of this file can be recognise in the following manner

[WORD][WHITESPACES][NUMBER][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES]

You will keep on consume in the above form until the end-of-file is encountered. The solution proposed is naive and doesn't takecare of incorrect input or incomplete input, but it will work in your case.

The key function is fscanf, either do a "man fscanf" in a unix system, or google for the online manual.

Below is a fragment codes to the idea

Code:
FILE *FP;
struct _point {
  float x;
  float y;
  float z;
};
struct _record {
  char nodename[20];
  int  nodeidx;
  struct _point pt;
};
while (not end of file) {
  struct _record r;
  int rv;
  rv = fscanf(FP, "%s%d%f%f%f", r.nodename, &r.nodeidx, &r.pt.x, &r.pt.y, &r.pt.z);
  if (rv != 5) {
    print error
  }
}

Now obviously it didn't take care of how to open file, and dynamically allocate memory, that will be an exercise for you.

The simplest dynamic data structure to cater to your needs is a Linked List, go do some research on it.

Here are some reading materials for you
http://www.cprogramming.com/tutorial/c/lesson15.html
http://en.wikipedia.org/wiki/Linked_list
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
i see that you are using rv!=5,which means count 5,this is much better than i used the what atof if else else etcs..it is similar to my teachers code with fscanf.

Okay, consuming input have quite a number of ways, you will need to think about how to consume input in a more robust way in the future.

I will give you something simple for you to consider

From the perspective of C, a file is just a series of bytes, there is no notion of multiple lines, that's what you perceive when the display print in multiple lines due to the CRLF limiters.

One way to look at your input is as follows

Code:
Vertex 1 0.120846 0.0669721 -0.0628787 Vertex 2 0.312241 0.00198185 0.0681098 Vertex 3 0.140731 0.444648 -0.0623968 Face 1 16563 41411 41412 Face 2 16567 16597 22256 Face 3 22245 16567 22256

All characters under this group are consider whitespaces '\t', ' ', '\r', '\n', '\f', '\v'

So actually your parsing of this file can be recognise in the following manner

[WORD][WHITESPACES][NUMBER][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES][FLOAT][WHITESPACES]

You will keep on consume in the above form until the end-of-file is encountered. The solution proposed is naive and doesn't takecare of incorrect input or incomplete input, but it will work in your case.

The key function is fscanf, either do a "man fscanf" in a unix system, or google for the online manual.

Below is a fragment codes to the idea

Code:
FILE *FP;
struct _point {
  float x;
  float y;
  float z;
};
struct _record {
  char nodename[20];
  int  nodeidx;
  struct _point pt;
};
while (not end of file) {
  struct _record r;
  int rv;
  rv = fscanf(FP, "%s%d%f%f%f", r.nodename, &r.nodeidx, &r.pt.x, &r.pt.y, &r.pt.z);
  if (rv != 5) {
    print error
  }
}

Now obviously it didn't take care of how to open file, and dynamically allocate memory, that will be an exercise for you.

The simplest dynamic data structure to cater to your needs is a Linked List, go do some research on it.

Here are some reading materials for you
Linked Lists in C Tutorial - Cprogramming.com
Linked list - Wikipedia, the free encyclopedia
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
i see that you are using rv!=5,which means count 5,this is much better than i used the what atof if else else etcs..it is similar to my teachers code with fscanf.

Normally scanf and its cousin variants functions are one of the first few beginners functions to learn since they allow beginners to be able to consume and validate inputs of various forms without going into the explanation of how parsing are done.

They are analogous to Scanner class found in Java5 and later, before Java 5, you will need to perform parsing using the respective Type classes' parse* methods, pretty much the same like atoi,atof in C.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
//can save the vertex into the vertex array,

but from what i know array is only like this:

#define MTHS 12 /* define a constant */
int days[MTHS]={31,28,31,30,31,30,31,31,30,31,30,31};

the C course I attended before never teach me malloc,
it goes up to the C processors only
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
but there is a problem,

Face 1 16563 41411 41412

these face are references to the vertex,

I still needed to store in linked list and iterate through after the fully iterate stored into the linkedlist
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
//can save the vertex into the vertex array,

but from what i know array is only like this:

#define MTHS 12 /* define a constant */
int days[MTHS]={31,28,31,30,31,30,31,31,30,31,30,31};

the C course I attended before never teach me malloc,
it goes up to the C processors only

The above is the static way of declaring and initialising arrays. To explain deeper, these data structures while exist in the memory is a fixed data segment that cannot be expanded.

Malloc doesn't create array, MALLOC is in short for MEMORY ALLOCATION. What you want to do with that block of data is totally up to your discretion. Put a pointer pointing to this block of memory and it becomes whatever you want it to be.

Be very flexible with C pointers and memory, they are very powerful yet very dangerous at the same time.

Let me show you an example
Code:
void *p = malloc(1000);
int *i = p;
char *c = p;
long *f = p;

Effectively the same block of memory can be an integer array, character array, long array all at the same time. The only problem is obviously, if I say c[0] = 'A'; what will printf("%d", i[0]) shows, do you think you will see '65' since the ASCII of 'A' is 65 ?

Almost likely you will not, because C don't give a "hoot" what you store in a memory block and it most certainly don't ensure it is semantically and logically correct. It is just blindly following what you want it to execute.

If you want to deal with dynamic data, you need to deal with dynamic data structures. Then you will also need to understand how to allocate and deallocate memory blocks and the rules of engagement to where memory is to be allocated and who is responsible to deallocate. All these will come in time, meanwhile go read up how to perform simple memory allocation and play with it by setting value to it and printing value out of it.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
//can save the vertex into the vertex array,

but from what i know array is only like this:

#define MTHS 12 /* define a constant */
int days[MTHS]={31,28,31,30,31,30,31,31,30,31,30,31};

the C course I attended before never teach me malloc,
it goes up to the C processors only

i feel like doing this but i can't,i got three different files vertex line is of arbitary 10k to 20k,

I recalled when I do in java,for linked list,i have to write a search function to tranverse through the linkedlist and get the vid(vertex id)

I also recalled in java,there is a hashmap(1,x),hashmap(2,x) as well,i am looking for simpler alternatives.

otherwise,i may have just call a FP to read through the entire file once again,save hassle of storing into linkedlist.

i would abandon using database for sure.

any idea how I can do this?

firstly,i have to calculate the normal of the vertex(i have did this)

that is why in each through i face to find the face normal by averaging the normal of the vertex,that is why must cross reference.
 
Last edited:

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
but there is a problem,

Face 1 16563 41411 41412

these face are references to the vertex,

I still needed to store in linked list and iterate through after the fully iterate stored into the linkedlist

You can first store them all under a single linked list and then post process of them, or you could at each consumption of a set of data, figure out between Vertex or Face and react as accordingly.

Now I remember you mentioned it's about an OpenGL application, then you might just want to store the Vertex first, then when it comes to the Face, call out he OpenGL glBegin(GL_TRIANGLES) function, then using the vertex references, fetch the required coordinates from the Linked List and call out to the glVertex3f function with the vertices coordinates

It's actually quite simple. Allow me to remind you that Linked List construction is fast and easy, but traversing it is slow. Hence one way is after you exactly know how many entries there are, create an equivalent array of same size and dump from the Linked List to the array so that you can perform random access fast.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
i feel like doing this but i can't,i got three different files vertex line is of arbitary 10k to 20k,

I recalled when I do in java,for linked list,i have to write a search function to tranverse through the linkedlist and get the vid(vertex id)

I also recalled in java,there is a hashmap(1,x),hashmap(2,x) as well,i am looking for simpler alternatives.

Lets just say you have downgraded from a very high level construct programming language called Java into a very low level construct programming language called C.

It's high time you discard away the semantics and casualness you get from Java because they don't exist in C natively. That's why developers in C are way more effective and hardcore than developers in Java mainly Java offers a very well defined language construct and an assorted web development SDK, which C doesn't offer it natively.

However C does have libraries contributed from a lot of open source libraries that does basic things like data structure libraries and algorithm engines. You will need to get up to speed to use these libraries properly.

I don't do C natively as part of my career path, so I wouldn't know a lot of such libraries.
However I know a couple since I often work with unix systems and often need to compile utilities. Gnome Library (glib) is one of them, Apache Portable Runtime (APR) is another.

Normally C projects grown over the years either implement their own utilities or uses a couple of freely available open source projects base layer as part of the foundation.

Welcome to the hardcore development arena :)
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
I looked back 3 years ago,

there is this code,

public static Customer findCustomer(Customer c) {

Customer foundCustomer = null;

// Searches for duplicated object based on E's compareTo attribute and returns if found.
Node<Customer> cur = customerList.head;

while (cur != null) {

// Get customer object if found
if (cur.item.getAccountId() == c.getAccountId()) {
foundCustomer = cur.item;
}

// Go to next Customer object
cur = cur.next;
}

return foundCustomer;

}

I remember I got difficulty implementing it in one of my assignment and failed it

I was thinking how I can implement a compareTo in it.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
I looked back 3 years ago,

there is this code,



I remember I got difficulty implementing it in one of my assignment and failed it

I was thinking how I can implement a compareTo in it.

I think you really need to get hold of a C programming book and steer your programming methodology around C instead of keep on using Java or any Object Oriented approach to solve your problem.

While the concept of linked list and data structure don't different between programming languages, the approach to implement them often depends on the language construct available.

In C, you don't have methods to class or to objects, you have functions that operate on their own. So don't keep on think of operations related to the entity you are operating on.

Rather think in the way that how you can reuse codes in a function operating on entities of similar types.

Below I will give you a simplistic generic linked list in C, but it is not beginner's kind of code that you will see in beginning C programming kind of books since those kind of linked list are very much cater to the kind of data that will be stored in it.

Here the codes below assume NOTHING about the data except it is a void pointer to a block of data. The data can be a simple integer, a float value, a structure, or even another linked list inside each node.

It make use of function pointer having certain interface to allow you as a programming using this linked list library to provide your own implementation on how searching should be performed, how display should be perform. C has no inheritance concept, hence you can't really overwrite the "toString" method(found in Java Object class), which doesn't exist at all. The use of pointers pointing to function will allow such flexibility, which is what you see below.

This is one way to code generic containers. I must say it is rather cryptic for first exposure to function pointers, but if you want to work in C, pointers are unavoidable concepts that you MUST GRASP it.

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

typedef struct _NODE {
  void *data;
  struct _NODE *next;
} NODE;
typedef NODE* pNODE;

typedef struct _LIST {
  pNODE head;
  pNODE tail;
  int size;
} LIST;
typedef LIST* pLIST;

typedef void* (*SEARCHFUNC)(pNODE node, void *key);
typedef void (*DISPLAYFUNC)(pNODE node);

pLIST llist_create() {
  pLIST p = malloc(sizeof(LIST));
  p->head = NULL;
  p->tail = NULL;
  p->size = 0;
  return p;
}

pLIST llist_free(pLIST* pplist) {
  free(*pplist);
  pplist = NULL;
  // is this good enough to free a list ?
}

pLIST llist_add(pLIST l, void *data) {
  if (l == NULL || data == NULL) return NULL;
  if (l->head == NULL) {
    l->head = l->tail = malloc(sizeof(NODE));
  }
  else {
    l->tail->next = malloc(sizeof(NODE));
    l->tail = l->tail->next;
  }
  l->tail->data = data;
  l->tail->next = NULL;
  l->size++;
  return l;
}

int llist_size(pLIST l) {
  return l->size;
}

void* llist_find(pLIST l, void *key, SEARCHFUNC f) {
  pNODE n;
  void *data = NULL;
  if (l == NULL || key == NULL || f == NULL) return NULL;
  n = l->head;
  while (n != NULL) {
    data = f(n, key);
    if (data != NULL) break;
    n = n->next;
  }
  return data;
}

void llist_display(pLIST l, DISPLAYFUNC f) {
  pNODE n;
  if (l == NULL || f == NULL) return;
  n = l->head;
  while (n != NULL) {
    f(n);
    n = n->next;
  }
}

void display_mynode(pNODE n) {
  if (n != NULL) {
    printf("%d", *((int*)n->data));
    if (n->next != NULL)
      printf(", ");
    else
      printf(".\n");
  }
}

int main(int argv, char **argc) {
  int data1 = 1;
  int data2 = 10;
  int data3 = 100;
  pLIST mylist = llist_create();
  llist_add(mylist, &data1);
  llist_add(mylist, &data2);
  llist_add(mylist, &data3);
  llist_display(mylist, display_mynode);
  return 0;
}
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
thanks,

i am studying for quiz searching for answer on this now:

what is (GL_modelView)

what is difference between GL_Triangle_strip and GL_Triangle

why the need to loadIdentitymatrix first

what is glmatrixmode
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,300
thanks,

i am studying for quiz searching for answer on this now:

what is (GL_modelView)

what is difference between GL_Triangle_strip and GL_Triangle

why the need to loadIdentitymatrix first

what is glmatrixmode

This is where you wanna be reading OpenGL 4 Reference Pages

GL_TRIANGLE_STRIP forms a strip of planes with sharing vertices.

GL_TRIANGLE forms a single triangular plane.

I think you really need to study on matrix transformation. I remember this part of the Mathematics is taught in school. Wonder it's JC or Secondary.

In the 3D virtual world, all transformation of all vertices in the 3D space is relevant to the global transformation matrix. Read up about the Identity Matrix here Identity matrix - Wikipedia, the free encyclopedia

Basically glLoadIdentity reset the transformation matrix to its Identity form so that everything starts reference from the world origin.

Here is the Official OpenGL reference guide that you may be interested in OpenGL Programming Guide: The Official Guide to Learning OpenGL, Versions 3.0 and 3.1 (7th Edition): Dave Shreiner, Bill The Khronos OpenGL ARB Working Group: 9780321552624: Amazon.com: Books

You require a holistic learning to how 3D modelling is done in OpenGL. How 3D projection, transformation, clipping, shading, and some of the basic concepts about 3D. OpenGL wouldn't give you all these insights. It's just an implementation, you need to first grasp the concept of when you transform, whose origin are you referencing from. It's quite confusing when I first started learning OpenGL. At some point, if you take reference from a different perspective, the order of the OpenGL functions actually only make sense reading backwards. If you take reference from a different perspective, then reading forward/top-bottom make sense.
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
because my assignment got extra weight,i need to calculate the normal of the face.

vertex* vertex_array;
Face * face_array;

these are array i thought of

I am looking back at my java assignment 3 years ago:

somewhere along the line:

static int custCount = 0;
static Customer customers[] = new Customer[custCount];

it has code that can copy an array to another array

this was done before I did the actually proceed to do the linkedlist example

if this can be done I will be glad.

currently i am looking at this:

http://www.cplusplus.com/reference/cstdlib/realloc/

update but strangely,when I do a vertex * array i check the size it is 4,
all i did is simply : vertex_array = (struct vertex*)malloc(0);
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
update i was trying out realloc now

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

typedef struct vertex {
int vid;
float x;
float y;
float z;
float nx;
float ny;
float nz;
}vertex;

int main(void) {
struct vertex* pArray = NULL; /* My array */
int nArraySize = 0;
nArraySize = 1;
pArray = (struct vertex*)realloc(pArray, sizeof(struct vertex) * nArraySize);
printf("%s",pArray[0]);//does work
printf("%s",pArray[1]);//cause program to crash
printf("%s",pArray[2]);//null pointer
return(0);
}


how do I check the size of the array?sizeof doesn't work?


pArray[0] does have something in it;
pArray[1] cause program to crash;
pArray[2] has null pointer;
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top