LinuxQuestions.org
Review your favorite Linux distribution.
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 03-10-2015, 07:32 AM   #1
BombaD
LQ Newbie
 
Registered: Jan 2015
Posts: 14

Rep: Reputation: Disabled
Accessing a string though a pointer array


I'm wondering how to access Buffer 1 and 2 via the pointer array;

Code:
char *BufPtrs[3];
        char Buffer1[64];
        char Buffer2[64];
        BufPtrs[0] = Buffer1;
        BufPtrs[1] = Buffer2;
        BufPtrs[2] = NULL;

I thought that if I were to access Buffer1 via BufPtrs[0], I would simply just put an * to it before printf()-ing or store it in a char[] (equivalent to a string).
 
Old 03-10-2015, 07:47 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
BufPtrs[0] is a char * so just use it that way.

clean compile with gcc -Wall

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

int main()
{
int i;

char *BufPtrs[3];
        char Buffer1[64];
        char Buffer2[64];
        BufPtrs[0] = Buffer1;
        BufPtrs[1] = Buffer2;
        BufPtrs[2] = NULL;

i=0;
Buffer1[i++]='Q';
Buffer1[i++]='W';
Buffer1[i++]='E';
Buffer1[i++]='\0';

printf("THING %s HERE\n", BufPtrs[0]);
exit(0);
}
 
  


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
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
C string as an array of chars and as a pointer to char Alien_Hominid Programming 16 05-18-2009 08:22 AM
about array and pointer seeLnd Programming 5 05-30-2004 07:56 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

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

All times are GMT -5. The time now is 02:28 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