473,394 Members | 1,866 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,394 software developers and data experts.

Retrieving a contain of an Array using pointer

Hi,

I was struggling the last 2 days with this C script supposing to open a list of strings (input as fopen(argv[1])and allowing to access to any element of the list. I created an array *gcm[10000] and a pointer *(*gcm_ptr[10000] = &gcm. However, when I try to list whatever n[i], it always gives me the last entry. Thanks in advance for any help.

Max
Nov 30 '13 #1
4 1159
HERE IS THE SOURCE CODE I USED.

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

#define NJFD 3059

main(int argc, char **argv){

FILE *fict;

unsigned short int *data_tab;

char line[1024];
char *cp;
char *n[10000];
char *(*n_ptr)[10000] = &n;

int m,i,quit;




/****** Open files and initialize ******/
if (argc != 2) {
printf("\n command line: <List File> \n");
exit(1);
}

if ( (fict = fopen(argv[1],"r")) == NULL ) {
printf("\n Cannot OPEN TAB file 1\n");
exit(1);
}


data_tab = (unsigned short int *)calloc(NJFD, sizeof(unsigned short int));
if (data_tab == NULL) {
printf("\n Not enough memory for tab data short int array \n");
exit(1);
}

m = 0;
quit = 0;
if(fict != NULL)
{
while((fgets(line, 1024, fict)) && (quit == 0))
{
cp = line;
while((*cp != '\0') && (quit == 0))
{
if((m < 10000))
{
n[m] = cp;
while((*cp != ',') && (*cp != '\0')){
cp++;
}
if(*cp == ',') cp++;

} /* end if */
else
{
quit = 1;
fprintf(stderr, "Error: Rows has exceeded 10000. Exiting program.\n");
} /*end else */

} /* end while ((*cp */
m++;
} /* end while ((fgets */

fclose(fict);

} /*end if (fict */
else
{
fprintf(stderr, "Error: Unable to open textfile.\n");
}

for (i = 0; i < m; i++) printf("List %d: %s\n",i+1, (*n_ptr)[i]);

if (data_tab != NULL) free(data_tab);

}
Nov 30 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
*gcm[10000] and a pointer *(*gcm_ptr[10000] = &gcm.

gcm is an array of 1000 pointers.
gcm_ptr is a pointer to an array of 1000 pointers.

Where are the strings exactly?
Nov 30 '13 #3
The strings are input from the (fict = fopen(argv[1],"r"); please, see the script above
Nov 30 '13 #4
weaknessforcats
9,208 Expert Mod 8TB
fict is a FILE pointer. A FILE is not an array of strings

You have to read the string into an array like line[1024].

Then you need to allocate a new array of 1024 and copy line into the new array. Then you can assign the address of the new array to n[m].

The way it is now, line is assigned to cp but this only assigns the address of line[0]. Since line is a local variable the address of line[0] never changes and so cp never changes either. The array n elements are all the same address. The address of line[0].

Usually, malloc is used to allocate a new array of 1024 before each read. The address of the array would go into n[m]. Then you would read directly into n[m].
Dec 1 '13 #5

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

Similar topics

15
by: fdunne2 | last post by:
The following C-code implements a simple FIR filter: //realtime filter demo #include <stdio.h> #include <stdlib.h> //function defination float rtFilter1(float *num, float *den, float...
2
by: dam_fool_2003 | last post by:
Array element's memory reference is continuous. What happens when I treat a pointer as an array? Is the pointer now used, as an array element memory reference is continuous? int main(void) {...
9
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
11
by: Sontu | last post by:
Consider the following code: int main(void) { char buffer; func(buffer); } void func(char *bufpas) {
7
by: Kathy Tran | last post by:
Hi, Could you please help me how to declare an araay of pointer in C#. In my program I declared an structure public struct SEventQ { public uint uiUserData; public uint uiEvent; public uint...
31
by: arun | last post by:
suppose i have a pointer to an array of integers.can i initialize each member of the array using pointers?plz explain
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
1
by: geervani | last post by:
hello, Can anybody just tell me how to scan or input a 2d array using pointer i e indirect reference. I am allocating memory the array dynamically. the statement i am using is int *a;...
3
by: vaibhavkanwal | last post by:
Hi, i am trying to initialize a dynamic array and use this array to conduct linear search. I know about new and delete but as a pointer can be incremented by the sizeof its dataype, i wanted to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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
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.