C :: Trying To Use Array Of Pointers To Alphabetize String
Dec 8, 2013
I am trying to alphabetize 3 different strings by comparing the first letter in each one. It will work for the first two names, but when I try to print out the third, I keep getting errors and I don't know why?
Code:
#include <stdio.h>
char personName();
int main() {
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)
I have been working on this program that reads an input file, then reads it, prints its averages and min and max, then writes a new file which organizes the names alphabetically and gives them a grade. I could assume the grade print in the new file could be easy enough with if statements, but I am stuck on how to get the names organized and print them by first name.
Having some trouble with basic stuff while learning C. My purpose was to flip the pointing order, so it would point to the string from the end to the start. didn't work so well using **str as well.
My teacher has done a very poor job of teaching us anything this year. When he taught us for loops, he wrote one on the board, didn't explain any of it, then said now that you know for loops we can implement them in a code. but anyway, we need to write a code for converting a string to an int and all the examples i find on the internet use pointers but we aren't allowed to use those yet.
The snippet below (or similar) compiles and runs OK but I am using Visual Studio C++ compiler. Are the lines where .nameFirst and .nameLast assigned kosher in ANSI C?
Also I am concerned about the memory allocation for these string constants. Does the runtime system put them on the heap? It doesn't seem that they are really constants since they are not defined before runtime.
So I'm writing a function isPalindrome() that can accept a string as an argument, and copy from it only the alphabetic characters in the argument (original) string to another string named alpha_array which contains only the alphabetic characters. Then the function should be able to call the isPurePalindrome function to determine if alpha_array is an ordinary palindrome.
The problem is that when I call isPalindrome in main, the program crashes.
Here's the code I have for isPurePalindrome and isPalindrome:
Code:
/* 1 */ int isPurePalindrome( const char * sentence ) // Can accept strings, array and pointer arguments { // Declarations
i've been writing some code for an assignment and it is mostly about pointers and string manipulations. It runs but crashes and I think it might be from over- valuating some strings maybe not. I have written in check points to make sure each function passes through but quits at findFirstPath loop, I had kept running the program through as i added more and more code. It had stopped when I near finished I believed it to just be because I hadn't finished the functions I called.
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
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.
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):
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:
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).
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.
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.
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.
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.
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.
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.
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:
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(){
#include <stdio.h> void Swap(int *x, int *y); int *Largest(int *array, int size); int main() { int a, b, i; int c[10]; int maxaddress; }
[code]...
My swap function works fine, but I am trying to find the ADDRESS of the largest element in my array and I am getting an error using gcc on my "return &largest;" and my printf line in my main function.How can I fix this and return the address of the largest?