LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2016, 07:08 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Question How to fill-up an pointer to an array of char in C


I'm writing a little code, nothing fancy, which gets the open file from /proc/<pid>/maps:

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

int main()
{
	FILE *file;
	char filename[1024];
	char input[4096];

	snprintf(filename, sizeof(filename), "/proc/1/maps");
	file = fopen(filename, "re");
	if (file) {
		while (fgets(input, 4096, file) != NULL) {
			char *pos;

			pos = strstr(input, "/");
			if (pos != NULL) {
				size_t len = strlen(pos);
				if (pos[len - 1] == '\n')
					pos[len - 1] = '\0';
			}
		}
	}

	return 0;
}
I would like to put all 'pos' into an char array or point char array, as you wish. I've tried some 'ideas', but all end-up with segfault or errors.

Never used a proper char array, so really not sure how to. Can anyone show me how or point me in the right direction?

when I've tried my luck, I've used something like:

Code:
char **s_array;
int count = 0;

s_array = calloc(1, sizeof(char *));
...
strcpy(s_array[count], pos);
count++;
s_array = realloc(s_array, (count + 1) * sizeof(char *);
...
but this didn't really work.
 
Old 04-13-2016, 09:58 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
You need an array of pointers to arrays of characters (C strings). The outer array is of unknown length, so you can either read the file twice: once to determine the number of lines and then to read each line, or you can create an array that is "big enough"

Code:
char **s_array = calloc(4096, sizeof(char *));  // enough for 4096 strings
Then as you read in each string, allocate space for it and add it to the array.

Code:
size_t len = strlen(pos);
if (pos[len - 1] == '\n') {
    pos[len - 1] = '\0';
    len--;
}
if (count >= 4096)
    return EOVERFLOW;
s_array[count] = calloc(len + 1, sizeof(char));
strcpy(s_array[count], pos);
count++;
 
1 members found this post helpful.
Old 04-13-2016, 10:44 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Sorry, but in calloc isn't the fist argument no_of_elements? Or am I reading wrong the documentation?
 
Old 04-13-2016, 12:05 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Never mind it simply takes the product of the two values
Also sizeof(char)==1. Plus there is a handy function called 'strdup'
 
Old 04-14-2016, 06:00 AM   #5
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Thanks guys, all worked.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Returning char pointer from function which has array of char pointer srinietrx Programming 3 10-30-2015 03:55 AM
[SOLVED] C - returning char array pointer CincinnatiKid Programming 5 01-06-2014 04:56 PM
How do I point a char pointer to a part of another char array? trist007 Programming 8 11-06-2010 07:56 PM
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
How can I assign to a pointer array like char *args[]; ? haydari Programming 3 04-09-2007 11:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration