C :: How To Stop Passing Char Array Index As Pointer
Dec 23, 2013
I am trying to write a light weight printf style function.
I have got this far:
Code:
void println(const char *txData){
LOG(__PRETTY_FUNCTION__);
UARTPuts (LPC_UART0, txData);
}
void miniPrint(const char *format, ...)
{
unsigned int index = 0;
va_list argptr;
va_start(argptr, format);
[Code]....
I understand why I think. When I am passing the reference to the array possion it is outputting everything up to the next /0. So my question is how do I stop it?
I dont have much choice as to how the output wants it:
Code: void UARTPuts(LPC_UART_TypeDef *UARTx, const void *str)
Thats library code, so I dont want to change it. I.e I have to pass an address into println.
View 1 Replies
ADVERTISEMENT
Nov 23, 2013
In the below program, When the getline function is called, it passes a char array of size 1000 by VALUE. It must have passed by value because there is no pointer or reference in the argument list of the getline function definition. And if that's the case, when exiting the getline function, isn't the s[] char array destroyed? And if it is destroyed, then when we reference line in the copy function, what are we actually copying?
#include <stdio.h>
#define MAXLINE 1000/* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input line */
main() {
[Code] .....
View 3 Replies
View Related
Mar 30, 2014
I have program where i have to check to see if file exist, if it does not then it needs to be created. SO I have a read file that works fine, if th efile exist it reads whats in it, if it does it says the file does not exist. Now Im trying to creata function that creates the file if it doesnt exist. so in my read function when the person enters the name of the file to be checked for..I pass that name to a variable called name..Hoping that I could then pass it into my create file function if it does not exist..and use that variable to pass the name they entered into the createfile array..called filename.. but I am having trouble because i get error when i try to pass from a string name to char array.. even when I change the varialbe name to char, or char [256] it will not work.. I try to fing a way to convert th string to a char using the strncopy function but still no dice..here the code i have for the createfile funciton
void CreateNew(ofstream & FileNew)//Create file
{
char filename[256];
string name;
[Code].....
How can i do this without having to ask the person to enter the file name twice..
View 12 Replies
View Related
Nov 20, 2013
what I am trying to do is to pass to a function the address of an array of structs, so I can later modify the items within the struct, within the array
Code:
typedef struct { //A struct of name auctionint bidder;float bid;} auction;
void myFunction (auction * auctionItem[]){(*aucItem[x]).bid = y;(*aucItem[x]).bidder = z;}
int main(){auction theItems[10];
myFunction(theItems);} Where x, y, and z can be any number.
When I try to run my code the IDE (I'm using Code::Blocks 12.11) does not give me any errors, but it does give me a warning:
warning: passing argument 3 of '<function name>' from incompatible pointer type [enabled by default]
and the note:
note: expected 'struct <struct name> **' but argument is of type 'struct <struct name> *'.Also, when I run the program, it will crash and return garbage.
View 6 Replies
View Related
Sep 7, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CMD_LINE 500
void tokenize(char *cmd_ln, char *fun_tknzd[], int *argument_cnt);
[Code] ....
I am trying to pass the value of fun_tknzd to str_tknzd
View 4 Replies
View Related
Apr 24, 2014
I can clearly understand how to pass a dynamic array to a function and modified within the function..
My aim is to get back the array initialized as I am trying on the next sample of code..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <fstream>
using namespace std;
#define N3//number of bodies
void initialise( double *M[], double *X[], double *y[])
[code]....
View 14 Replies
View Related
Oct 10, 2013
I have been assigned the following task and I am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is.
The task is as follows:
Write two functions with the following function prototypes:
int my string len(char str[])
void my string cat(char dest[], char src[], int dest size)
Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string
And this is my code so far:
/* A program to demonstrate string concatenation */
#include <iostream>
#include <string.h>
using namespace std;
int my_string_len(char str[]){ // function to calculate length of a chracter array
int len = 0;
[Code] ....
View 8 Replies
View Related
Aug 31, 2013
I like to use a Pointer to char array. And then I would like to do a Pointer Arithmetic by incrementing the Pointer. Finally I would like to see the Addresses of the Pointer to each of the char Array Elements. I had created a program below, but I am not getting any Addresses from my Pointer.
#include <iostream>
using namespace std;
int main () {
int ArraySize;
char ch[]= "This is a Char Pointer";
char* iPtr = ch;
[Code] ....
View 3 Replies
View Related
Jun 7, 2013
Alright, so I have a code that's not giving me errors, but it doesn't seem to retain what I put into an array. Not sure If I'm missing something...
Code:
#include <stdio.h>
int main (void)
{
const char *pointer;
const char alphabet[] = "ABCDEFG";
pointer = &alphabet[5];
printf("pointing to %c of the alphabet
", pointer);
return 0;
}
Trying to get my pointer to return the letter in the [5] spot or "F". Not receiving any errors when compiling, but I seem to get different answers every time I run it.
View 5 Replies
View Related
Aug 10, 2013
I can assign values to pointer character array like this...
Code:
char *array[4]={"abc","xyz","dgf","sdt"} ;
but the case is i don't know how to assign strings through key board ???? with using gets ,getchar or other suitable function
View 2 Replies
View Related
Apr 20, 2013
I have an array of char pointers:
Code: char *input_args[MAX_ARGS];
And I have this function:
Code: BOOL parseArgs(char **input_args[], input arg_num);
I am trying to pass a pointer to this char pointer array like this:
Code: parseArgs(&input_args, args_num);
But the compiler is complaining:
Code: warning: passing argument 1 of 'parseArgs' from incompatible pointer type ...
note: expected 'char ***' but argument is of type 'char * (*)[20]'
Tried a bunch of stuff but nothing works.
View 4 Replies
View Related
Feb 4, 2014
Why does this code doesnt work?
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string {
char* ptr;
[code] ....
View 1 Replies
View Related
Nov 28, 2014
I am trying to store data in a struct to be able to read it latter . have problems initializing this.
struct FoodAndDrink {
struct Food {
char* CannedGoods[2] = {
"Canned Spaghetti",
"Canned Tuna",
[code] .....
View 7 Replies
View Related
Jul 13, 2013
I have function that looks like this myfoo(char* Name) Now i want to compare this name to another one . But the another name is a pointer . This my code :
bool Tribe::RemoveSurvavior(char *H_Name) {
const char *p;
p=SurpointArr[i]->GetSurvivor_Name();
}
I need to compare if p is same as H_Name.
Mine is do it with for on each element but when i use sizeof it gives me size of char and not real size of the name.
View 6 Replies
View Related
Feb 22, 2013
I have this function in a class: and a private declaration: how can I copy the parameter "ProductName" to allowedProductName. I tried all combination and I can't get it to compile.
private:
StatusPanel &statusPanel;
char allowedProductName[MAX_NAME_LENGTH];
[Code].....
View 9 Replies
View Related
Apr 25, 2014
I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.
Here is a snippet:
Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}
Visual studio is saying that the return type doesn't match.
View 3 Replies
View Related
Feb 23, 2013
I am using visual studio 2012.....in below code i m writing data in to a test.txt file but i dont know with which key file stop accepting char...i tried ctrl+z and ctrl+d but not working ....
Code:
#include<stdio.h>
#include<conio.h>
main(){
char ch;
FILE *ptr;
[Code] ....
View 7 Replies
View Related
Nov 5, 2014
I need understanding the logic behind this function. The function is supposed to "Return a pointer to the character at the index given" . For example, what exactly are we declaring at "(char * const c, int index)"? where does "index" come from or equal to in the body of the function?
Code:
1
2
3
4
5
6
7
8
char * GetValueAtIndex(char * const c, int index)
{
int i = 0;
char *p = c;
while (i++ != index)
p++;
return p;
}
View 2 Replies
View Related
Sep 8, 2014
#include<iostream>
using namespace std;
int main(){
int size_col = 12;
int size_row = 12;
char months_array[12][12] =
[Code] ....
I am having problems with passing a block of a char to a variable, so i could print it out.
It gives me an error: value of type *char cannot be assigned to a entity type char.
View 7 Replies
View Related
Nov 16, 2013
I'm having trouble with passing a character array between functions of the same class. I have a function, buildGraph, that calls function getNextLine. The getNextLine essentially just retrieves the next line of an input file and stores it into a "char line[80]". However when I try to use "line" in my buildGraph function, it has nothing in it.
Here's my code:
Class
#define NUMNODES 10
using namespace std;
#pragma once
class Prog3Graph
[Code] ....
View 4 Replies
View Related
Dec 9, 2014
My program takes the values from one array and searches for their index position in another array (linear search algorithm). This is an example of the issue im having(its not part of the actual code below)
a[]={1,2,3,4,5,6}
Arr[]={1,2,2,3,4,5}
If it finds 1 in arr, it returns 0, which is fine but if it finds 2 in arr it return 1 and 1 instead of 1 and 2.
for (int q=0; q=size2;q++) {
int rs=secfunc(array1;size1;array2[q])
if(rs>=0) {
cout<<rs << "";
[Code] .....
View 4 Replies
View Related
Feb 29, 2012
Let's say I've got something like this:
struct Box{
//something
};
typedef struct Box* pBox;
[Code]....
function fun recieves the address(which is NULL) and then allocates the memory for the Box; Let's say I cannot return the address of new allocated p and I can't also use that pointer p(from main) without passing it into a function.
Q: How can I make it that I could operate in function "fun" as I operate on orginal pointer p(from main), right now I'm just passing the address to my function but I can't change the 'global' pointer p ;(.
I remember in pascal it's like:
"function(var pointer:[pointer to sth])"
and all is done.
View 14 Replies
View Related
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
Apr 17, 2014
I was trying to debug a code which was behaving in an abnormal way
Code:
#define NOOFELEMENTS 32
unsigned char array[NOOFELEMENTS];
unsigned char array1[23];
init() {
for(i=0;i<=32;i++)
{
array[NOOFELEMENTS] = 0
}
}
I could trace the above one of the mistakes where the array initialization is crossing the array limits and writing into array[32] which is not available. My question does it overwrite into array1 as it is declared below array or it can write into any other location.
View 4 Replies
View Related
Aug 24, 2014
I am trying to wright a program that takes student grade data from a command line file, calculates a final grade, and copies the final grades to an output file. So far I have two functions, one that creates a student structure by allocating memory for it and returning its address, and one that should take that address and fill it with data from a line from the input file. My ultimate goal is to use a linked list to connect all the structs, but for now I just want to get the functions working. When I run what I have so far, I get an error C2440 (using visual 2010) that says "cannot convert from 'cStudent *', to 'cStudent', and points to the line where I call my fill function. How should structure pointers be passed?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student // Declaring student structure globally.
[Code] .....
Also, here is a sample of what a line from the input file would look like:
Bill Gates, 60, 54, 38, 62, 65, 60, 50
View 2 Replies
View Related
Oct 14, 2014
I'm trying to pass the pointer of a dynamic array into a template function, but it keeps telling me there is no matching function to call because the parameters I'm passing in are wrong. how to make the function accept the pointer.
//main
int main()
{
srand(unsigned(time(NULL)));
int size;
int *list;
int *listCopy;
[code].....
View 4 Replies
View Related