C/C++ :: Accessing Array Elements Not Working

Jan 16, 2015

I am trying to retrieve the elements in an array to print them. I am pretty sure I have everything right, but it is not working. There are 5 values in the array {3, 4, 10, 5, 6}, but the first two elements (supposed to be 3,4) are coming out a 0,-2654238590. The last three elements are coming out correct.

void addDynArr(DynArr *v, TYPE val) {
/* FIXME: You will write this function */
assert(v != 0);
//resize if necessary
if(v->size >= v->capacity)

[Code] ....

View 6 Replies


ADVERTISEMENT

C++ :: Accessing Elements Of Array Dynamically

Jul 26, 2014

I want to access the elements of my array dynamically. So far I've only figured out how to do this manually. if I tried it like this my code would work but there should be a better way right?

View 10 Replies View Related

C/C++ :: Accessing Elements Of Struct Array

Dec 9, 2014

I've been working for some number of days on this code to take information about movies from both a file and the user, store the infos in an array of structs, and at the end, write all the info out to a file. I'm having some problems with an error message reading:

"prog.c:102:11: error: subscripted value is neither array nor pointer nor vector"

this error occurs in many lines (which I will label specifically below -- they are everywhere where I am trying to access/modify an individual element of a struct element of the array).

A few examples of where I am having the problems are lines:
39, 52-55, 70, 72, and 86 (and more of the same exact variety).

I am obviously rather systematically doing something wrong, but I am quite certain all of these are the exact same mistakes.

I pull up also 2 or 3 other errors, but I don't think they are related and should be able to fix them quickly once I work out this conundrum.

#include <string.h>
#include<stdlib.h>
#include <stdio.h>
int moviecount =0;
typedef struct{
int year;

[Code] .....

View 5 Replies View Related

C++ :: Pointer To Array - Accessing Elements?

Mar 31, 2013

Code:
int arr[10];
int* p = arr;
int (*p2)[10] = &arr;

So, pointer p is a pointer to an array, I can use it to access elements of arr as in *(p+5).

Pointer p2 is a pointer to an array of ten integers. What is it for, how can I use it to access elements of arr?

View 4 Replies View Related

C/C++ :: Accessing Elements From Arrays

Feb 9, 2014

I'm confused about accessing elements from arrays. Supposed I have an array of pointers to char:

char *names = { "John", "Rose", "Steven" };

To access one of the strings, should I use names[ 0 ][ i ], where i is an index in the set ( 0, 1, 2 ), or should I use names[ i ]? I would think it would be the first option, because this array has 1 dimension that contains others arrays, right?

View 8 Replies View Related

C++ :: Accessing And Working With Vector From Multiple Threads

Oct 20, 2014

I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.

The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.

However, if I need to switch from vectors to something else then I am open to that as well.

The following example below compiles but crashes rather quickly because it is not thread safe.

How I can fix the example below which I can then apply to my larger project?

#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {

[Code] ....

View 1 Replies View Related

C++ :: Accessing Nested Elements Of Vector

May 1, 2015

so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container example...

Code:
std::set<vector<int> > my_tuple;

How do i access individual elements of lets say the first vector in the set! Or the last element in the 3rd vector?

View 7 Replies View Related

C++ :: Copy Elements Of One Stack To Another - Push Method Not Working

Oct 27, 2014

I am trying to write a method (copyStack())that copies the elements of one stack to another. So I am using a Type variable and a while loop, assigning the top of the first stack to the variable, and then using the push method to push the Type variable into my second stack (then poping the first stack). However, whenever it comes time to print the second stack (the one I am trying to copy into), nothing shows up. I know that the program is reaching the copyStack function, and I know that when I put a regular string in for the Type variable, it passes that string along. But for some reason, when I use the variable, nothing happens! Here's what I've got...

header file containing stack manipulators:

//Header File: linkedStack.h

#ifndef H_StackType
#define H_StackType
#include <iostream>
#include <cassert>
using namespace std;

[Code] ....

View 3 Replies View Related

C :: Segmentation Fault When Accessing Array

Aug 27, 2014

Write a program that reads in the names and the ages of ten people. Store these data in two arrays (make sure that the entered names are not longer than the array size you choose). Then produce a table of ten lines, with each line giving the name and age of a person along with the (positive or negative) deviation of that person's age from the average age. The code I wrote to fill in the arrays looks as follows

Code:
#include<stdlib.h>
#include<stdio.h>
int main()
{ // begin main()
// array length

[Code]....

When I compile it, it gives me warnings that few of the variables I declared aren't used, but that is not affecting the program at all. When I run the program, it allows me to pass the names and ages into the arrays, but when it comes to displaying them, I'm getting a 'Segmentation fault'. I used very similar code in Java to write the program, and it worked fine so what is the problem with C then?

View 8 Replies View Related

C++ :: Accessing Array In A Thread (boost)

Nov 22, 2012

I was wondering if it was possible to access data from an array inside a threaded function after it has finished executing?

Take this example:

Code:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;

[Code]....

After workerFunc has finished, how could I access the array arr? Is there any way to get (3,4,5,6,7) back into main()?

View 5 Replies View Related

C :: Accessing Values Stored By A Pointer Into Array

Mar 7, 2013

I am writing a C program to access a string into an array from a pointer array in Visual Studio 2010. Program is given below:-

Code:

#include <stdio.h>
#include <string.h>
void main() {
char data_a[6],data_b[6]="ABcde",*ptr;
ptr=&data_b[0];
data_a[0]=*ptr;
printf("%s",a);
}

I am getting some junk character first and then my actual data on console window. I want to where I am getting wrong and how to resolve it.

View 4 Replies View Related

C++ ::  accessing / Assigning Values Of A 5 Dimensional Array

Feb 8, 2014

I'm okay with 2-dimensional arrays, but when I get to 3 or more, I can't seem to wrap my head around how to assign and/or pull values from specific parts.

To give an example, let's take the following example:

We know that a player can take up to 5 total quests, and each quest can have a max of 5 tasks.

Let's assume I have the following multi-dimensional array holding all of a players quest data:

quests[0] = 78;// Store the questID
quests[0][0] = 3945;// Store the 1st creature ID
quests[0][1] = 2230;// Store the 2nd creature ID
quests[0][2] = 3045;// Store the 3rd creature ID
quests[0][0][0] = 2;// Store how many needed of the 1st creature
quests[0][1][0] = 5;// Store how many needed of the 2nd creature
quests[0][2][0] = 13;// Store how many needed of the 3rd creature

As we know, the above code can't be done. How do I assign certain values to each specific dimension?

View 6 Replies View Related

C/C++ :: Accessing Array At A Certain Memory Location Results In Seg Fault

Aug 4, 2014

I have a contiguous sequence of section headers in a file (all the data in the file is stored in void * data), where each section header is the same size and has the same fields in the same order. So it's laid out like an array.

I have to:

- Use an offset value and the number of section headers variables to identify the location and length of the section header table (these are hdr.offset and hdr.length respectively).
- The offset value shows the distance between the start of the file and the start of the first section header (so I need a pointer to the start of the first section header)
- Apply a typecast to location of the section header table to process it as an array of section headers.

Here is what I have done, but I am getting a segmentation fault. What am I doing wrong? How do I fix this?

typedef struct {
unsigned int name;
unsigned int type;
} SectionHeader;

[Code] ....

View 1 Replies View Related

C++ :: Accept Integer Array And Its Size As Arguments And Assign Elements Into 2 Dimensional Array

Jan 10, 2015

Write a program using user-defined function which accepts an integer array and its size as arguments and assign the elements into a two dimensional array of integers in the following format: If the array is 1,2,3,4,5,6, the resultant 2D array is

1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0

View 1 Replies View Related

C++ :: Write Function That Takes Array And Returns True If All Elements In Array Are Positive

Jan 21, 2013

Write a function that takes an array and returns true if all the elements in the array are positive, otherwise, it returns false.

View 6 Replies View Related

C++ :: Assigning Elements Of 1-D Integer Array Into 2-D Array Of Integers

Sep 27, 2013

I am having problem in writing the code for the problem "To assign the elements of 1-D integer array into 2-D array of integers such as if the array is 1,2,3,4,5,6 The resultant 2-D array should be like :

1 0 0 0 0 0
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
"

View 7 Replies View Related

C/C++ :: Difference In Array Name And Address Of First Elements Of Array

Aug 13, 2014

I have just started learning C language, I have read that array name is the pointer to the first element of array.

So,technically both a and a[0] should have different memory address and a should hold address of a[0],

But when I declare an array and print the address:

int a[5]={2,4,6,8,10};
printf("The address of a and a[0] are &d and %d",&a,&a[0]);
Output is: The address of a and a[0] are 2358832 and 2358832

Why I am getting the same result, shouldn't I be getting different memory address for both a and a[0].

View 2 Replies View Related

C :: Array Doesn't Seem To Be Working

May 26, 2013

I am writing a program that expands array list whenever they get too full...so far i have this:

Code:

#define DEFAULT 10
typedef struct ArrayList {
//array of strings
char **array;

[code]....

So, ArrayList *myList should return a pointer to the new arraylist or null if malloc fails. what exactly I need to set my maximum to, I know that it shouldn't be 0 and array[i] doesn't seem to be working either. I also am not sure if I am properly setting up null correctly for my array.

View 1 Replies View Related

C++ :: How To Add Elements To Array

Apr 5, 2013

I've a file that looks like this:

84484-37.96-Castor, Kathy
39050-69.68-Chandler, Ben
26183-70.84-Costello, Jerry

I have successfully read each element the id, grade and name into 3 separate array. Now i need to add a new student with an id and grade

How do i do this?

This is what I have.

int addStudent( int Iarray[], double dArray[], string sArray[], int newID,
double newGrade, string newName, int size ) {
char ready;
int index;
cout << endl;
cout << "Enter new student ID number : ";

[Code]...

View 4 Replies View Related

C/C++ :: Elements Outside Array?

Oct 27, 2014

I have a question regarding the elements of an array. Suppose I have a 3 by 3 string array (char x[3][4] ) , and I initialize all the elements to x's , the array would then look like this :

xxx
xxx
xxx

I'm curious if there will be a value if I try to access and element outside the array. As I have to write a code to determine if I have reached the end of an array. The only way I can think of is to border the entire array with o's , making it look like this :

ooooo
oxxxo
oxxxo
oxxxo
ooooo

Is there any other way to do this?

View 5 Replies View Related

C :: Concatenate Strings In Array (2D) - Function Not Working Right?

Sep 6, 2013

I have a function that concatenate the strings in an array(2D)

Ex 1: Sean Connery Micheal King James Wood

Result: SeanConnery MichealKing JamesWood ...

The concatenation function working correctly and displays correctly in the function. But if I make another function to display it, it shows this

Ex 2: SeanConnery Sean MichealKing Micheal JamesWood James..

It adds to first name. Why?

Code:
void Concatenation( char dest[200][13] ) {
// loop through and concatenation the strings
for(int i=0;i<200;i+=2) {
myStrCat(dest[i],dest[i+1]); // mystrcat is equalto strcat()

[Code] .....

View 4 Replies View Related

C/C++ :: Bubble Sort Not Working For Array Of Strings?

Apr 9, 2015

I am trying to sort an array of strings but when I pass it through my bubbleSort function, it gives me a segmentation fault. By print checking, I know that there are strings in the array "f" but it doesn't get sorted.

Note: The file I am putting into this program is a text file with 1000 lines

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

[Code].....

View 1 Replies View Related

C/C++ :: Why Matrix Array Will Stop Working When Compile It

Oct 8, 2014

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
    int a,b;
    int Rowsize , Colsize ;
    int A[Rowsize][Colsize];  

[Code] ....

View 1 Replies View Related

C :: Trying To Concatenate 2 Elements From Array

Oct 19, 2013

Trying to concatenate the 1st and 2nd element from an array of chars. Then this to be used as a hex value.

so for example
specs[0]='f'
specs[1]='4'

concatenate_value=0xf4

View 9 Replies View Related

C :: Reversing Elements In Array

Apr 15, 2014

Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in thearray . The function reverses the elements of the array . The function does not return a value .

Code:
void reverse(int a[], int num) {
for ( int i=0; i <= num/2 ; i++){
int temp = a[i];
a[i] = a[num-i-1];
a[num-i-1] = temp;
} }

This is supposed to be the answer but I'm not quite sure why this is. I understand everything up until the actual loop. For one, shouldn't "int i" be declared outside the loop (I thought perhaps this was an error in the solutions)?

The main thing that I do not understand is the conditional statement.

Code: i<=num/2;

I don't understand why the "num/2" is necessary here. Also I can't really remember but is there a command that actually reverses an array?

View 5 Replies View Related

C++ :: Setting All Elements Of Array To 0

Sep 18, 2013

I'm trying to set all the elements of my array to 0 but nothing seems to be working.

.h
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
ServerGroup(const int&);

[Code] .....

I want to set each element of the array in servers to 0 based on what is passed into size by numArray.

View 9 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved