C :: Array Of Pointers To Strings
Mar 6, 2015
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).
View 2 Replies
ADVERTISEMENT
Jan 13, 2014
I am trying to copy string Line BY Line from text file into array of pointers. lets say file has only two lines for example.
This is an apple.
This is another apple.
I want to copy both of these lines from file to array of pointers. Below is the code which i am trying to use for this.
Code:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <string.h>
using namespace std;
int main() {
char const *filePath = "test.txt";
[Code] ....
View 14 Replies
View Related
Feb 28, 2015
I need to calculate how often each letter appears in a text file from a function that is called from main() with an array of pointers to char and how many pointers there are in the array. The code i have so far is:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
[Code]....
I get no errors and it runs but it gives me the wrong output. I know my array is correct because when i print it in main() it is correct but each letter is incorrectly counted. When i give it the input .txt file of:
This is one line of a string
This is another
This is the third one
Wow heres another one
It counts a as 8
b as 1
c as 3
etc.
I have hand traced it and cant figure out why it isnt giving me correct values
View 4 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
Jan 3, 2015
I am trying to make a pointer to pointers for strings with malloc but my code isn't right.I tried something like that.
Code:
char **p;
**p=malloc(100*sizeof(char*));
for(int j=0;j<100;j++)
*(p+j)=malloc(10*sizeof(char));
how could i allocate memory is only needed for every string(as long as it is)? Cause in my code i allocate memory for 100 strings and for 10 characters for every string.
View 1 Replies
View Related
May 29, 2014
I have wrote the code below that compares to strings and sees if they are equals doesn't matter the order of the words . In the question we where asked not to use any library functions like the string functions in <string.h> and we have to do that all with pointers . I debugged my code and for some reason the first loop in the function keeps looping ...
#include<stdio.h>
int IsEqual(char* str1,char* str2);
#define SIZE 20
void main() {
char str1[SIZE]="my name is monaya",str2[SIZE]="name monaya is my";
if (IsEqual(str1,str2))
[Code] ......
View 4 Replies
View Related
Feb 13, 2013
This is my program and i dont know what is the better strategy to display the output perfectly align with the title, when i input a long variable or short the variable move and it does not align with its title. what can i do.
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
struct book {
[Code] ....
View 1 Replies
View Related
Jan 18, 2014
I'm trying extremely hard to understand pointers and I have the basic concept down.. I feel as though my knowledge of dynamically allocated pointers and pointers in general is not enough to understand the logic behind what I'm trying to do. The problem is that the donations array must be able to accept any number of donations. I've made it do just that, but there is also an array of pointers which must each point to the same element in the donations array. The program works if I assign int *arrPtr[100] for example, but it does not work if I try to dynamically allocate it to accept the same number of elements for donations entered by the user. Here it's the snippet
#include <iostream>
using namespace std;
//Function Prototypes
[Code]....
View 2 Replies
View Related
May 21, 2014
I am trying to initialize an array of pointers to an array of characters, I can do it in 3 lines but I really want to do it in one line at the same time keeping the #define.
3 lines initialization (can compile)
======================
#define A 1
#define B 2
char row1[] = {A|B, B, A};
char row2[] = {B, A};
char *test[]= {row1, row2};
1 line initialization (failed)
===============================
char *test[] = { {A|B, B, A}, {B, A} }; // <- how do i do this??
I do not want this because it waste ROM space
=============================================
char test[][3] = { {A|B, B, A}, {B, A} };
View 18 Replies
View Related
Mar 5, 2013
1. I finished reading a beginning C book, and in the section about arrays, it says that one string can fit in a character array (char arrayname[]) but there cannot be a string array (string arrayname[]) that have multiple strings. Is
Code: string arrayname[4] = {"one", "two", "three"}; not valid?
My compiler lets me run it and it works, but why is the book saying it's wrong?
2. I know you can represent multiple strings in a character array by:
Code: char newarray[10][4] = ("one", "two", "three");
because [10][4] indicates that there should be four newarrays created with a max of 10 characters each, but is
Code: string multiplestrings[10][4] = ("i love you", "hello come to me", "i don't get C"; "hello world", "what are arrays"; "i am happy", "I am learning how to code"); valid?
Does multiplestrings[10][4] basically create 4 string arrays that have a maximum of 10 different strings within each string array?
View 6 Replies
View Related
May 10, 2013
I am working on an assignment identical to another post from a couple years ago, for reference here is the thread:
array of pointers to structures sorting addresses by zip code
They way it is written on that thread is almost identical to the way the teacher implied to have it done (only wrote part of the input block). But I am having an error:
When it gets to the output section it outputs then next name along with the zip code... I tried strncpy and strxfrm but both cause more problems than they did work.
The last part of the project is to have the output put out in order of least zip code to most zip code (00000<99999), so this is causing me a real problem and I do not see what exactly is making this happen.
Here is my code (we dont HAVE to use gets but professor suggested using it for this assignment, next lab is to rewrite this using files and fgets rather than I/O redirection):
header.h Code: #ifndef lab_6b_7b_Header_h
#define lab_6b_7b_Header_h
//header file intiating other headers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
[code]....
I have not started the sorting code because I cannot get past this, but once I have proper zip codes I am sure I can make a sort function no problem.
I am using xcode with some breaks to read variables as various points and do not notice anything wrong until it makes it to the output functions, although this page briefly pops up between input and output functions when the breaks are up:
View 8 Replies
View Related
Aug 9, 2013
How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.
Code:
int count = 0;
struct addx {
int a;
[Code].....
View 1 Replies
View Related
Sep 27, 2013
I've created an Array of pointers to objects using:
Person ** A = new person * [arraysize];
When I intend to access a specific person do I have to do this? :
something = A->[i];
and when I want a specific object within my struct do I have to do this? :
something_else = A->[i]->random_int;
View 10 Replies
View Related
Feb 10, 2013
I'm just trying to get a handle on the uses of pointers here. Though clearly from my errors I'm missing a key concept. Here is my code: (You can assume that the array, "array_size" has values in it, I did this part in another function)
int main() {
bool **ptr_array;
int num;
int *array_size;
cin>>num;
[Code] ....
Once the program reaches the word[num] = false; some unhandled exceptions pop up.
I simplified my code a bit from my actual program and mixed up the loops, now the code should be in its correct form.
View 4 Replies
View Related
Feb 14, 2013
I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.
For example
struct info{
char firstName[15];
char lastName[15];
};
main() {
info person[4];
cout << "The third letter of the second persons first name is: "; // ?????
}
how would I define a pointer to point to the third letter of first name the second person in the "person" array.
View 2 Replies
View Related
Dec 5, 2013
my code:
int OKCount=0;
int WaitingCount=0;
int ReservationCount=0;
Flight::Flight(int capacity, int waitingMax) {
seats=capacity;
[code].....
reservations is a data member in the class flight as:
Reservation **reservations;
OKReservation is a derived class and its abstract base class is Reservation.
My problem is that the reservations array loses its value in other function
View 8 Replies
View Related
Mar 27, 2013
I am having trouble storing strings into an array. Basically I have a console program where it has an option. This is just an example;
Code:
char name[max];
char listofnames[maxname];
int counter;
....
printf("Add names
");
And how can I print it?
View 9 Replies
View Related
Feb 23, 2013
I am having this problem were the user types in a substring to search in the main string and replace it with another string
for example:
string: hello
sub string: el
replace: i
should print this: hillo
I tried using some string functions but I am having problems with that.
View 7 Replies
View Related
Feb 5, 2013
How do you use a for loop to iterate through strings to display them in a console?
View 3 Replies
View Related
Sep 26, 2014
#include<iostream>;
using namespace std;
int strlen(char []);
void strcat(char S1[], char S2[], int size_1, int size_2);
const int SIZE = 100;
const int MAX = 100;
[Code]...
i have been trying to do a function that unites strings entered by user, i need to do this without any other libraries but i keep getting an output of the first word and dots.
View 5 Replies
View Related
Feb 19, 2014
I was very much confused with managing the 3d arrays. Other than initialising it is tough for me to input the data into the 3d array at runtime. I have tried the every possible method i know untill now but i can't successfully input and output the data.
I have written a code by declaring the 3d array of char s[5][2][20] in main and passing it's base address into another function, lets say input(char (*p)[2][20],int ,int ,int). I have passed the 3 dimensions in declaration 5,2,20 to the input function i have used these 3 values as the subscript for the pointer p and tried to input the data. But i wasn't successful.
How to input the strings into the 3d array.
My preferences were:
1.The change of subscript value should take place in loop(eg:for).
2.Which indexes should be used for inputing the data through pointer.
3.Use either "scanf" or "gets" function to inputs the data and print using "printf" or puts.
View 1 Replies
View Related
Mar 6, 2015
What I'm trying to do with this code is an address book and I have an array of pointers which are returned by malloc whenever I need to add an extra entry in the address book. What I need to do is search for a specific entry using bsearch. I've got an inqSort() function that sorts the table and runs normally, but my program crashes when I try to use bsearch.
Code:
typedef struct {
char name[20];
char phone[14];
} abEntry;
[Code] ....
Every time an entry is inserted, I inqsort() the array so it's always sorted, and it works as expected. But when I try to call findEntryUI(); from the main() function, the program crashes after entering the name I want to search.
View 3 Replies
View Related
Mar 24, 2013
why when I print out "array[2]" nothing prints? It just prints blank space. My file definitely has text in it, but when I try to assign "text" into the array of pointers it won't show any text. I know fgets() appends a newline at the end of the string, not sure if that has anything to do with it, but I've tried printing everything that should be in "array" with a for loop and I get nothing.
Code:
#include <stdio.h>
#define FILEPATH "/home/user/Desktop/text"
int main(){
FILE *myFile = fopen(FILEPATH, "r");
if(myFile == NULL) return 0;
char text[100];
char *array[100];
int idx = 0;
while(fgets(text, 100, myFile)){
array[idx++] = text;
}
puts(array[2]);
}
View 2 Replies
View Related
Jun 11, 2013
I have a little problem with one of my functions. The function purpose is to get a number (n) and create an array (size n) with pointers to strings (each string length is 20 chars) and i don't know why but during the debugging i get a <bad ptr> message and this message :
CXX0030: Error: expression cannot be evaluated
This is my function:
Code:
char** getlist(int n) {
int i=0;
char **arr;
arr=(char**)malloc(sizeof(char)*n);
if (arr==NULL)
[Code] ....
View 8 Replies
View Related
Jan 29, 2015
I basically have some code that lets users register callbacks into a callback table at a specified index. There is one element in this table for each event that can trigger a callback. I basically do something like this:
In a header file
Code:
typedef struct _GMclient
{
GMcommunicator gcomm;
GMclient_callback callback_table[(int)MAX_CALLBACKS];
}GMclient;
typedef int (*GMclient_callback)(GMclient*, void*, void*);
Then I allow them to set the callback with a function that basically ends up doing this (in a .c file that includes the previous mentioned .h file):
Code:
void
GMclient_register_callback(GMclient *client,
GMclient_callback fxn,
int index)
[Code] ....
Then later when I need to invoke that callback, I pull it out of the table
Code: GMclient_callback *fxn = client->callback_table[CMD_INDEX];
However, I get compilation errors:
Code: src/gmanager_client.c:43:6:
error: a label can only be part of a statement and a declaration is not a statement...
View 4 Replies
View Related
Aug 4, 2013
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){
a= malloc ...
b = malloc ...
// fill a and b
return them back to the main function
}
void main(){
[Code]...
View 3 Replies
View Related