473,387 Members | 1,529 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

memcopying to a variable

15
Hi guys, I'm trying to copy the output from the for loop to a variable 'temp'

for(int i=1;i<4;i++)
{
printf("%s", 2 * i); //outputs - 246
}
printf("\n");

----------------------------------------------------------
I've tried this to only get 'segmentation fault'

char temp[20];
int a;
for(int i=0;i<10;i++)
{
printf("%d", 20 * i);
memcpy(temp[a], 2 * i, 1);
a++;
}
printf("\n");

---------------------------------------------------------
can anyone help me? Thanks
Apr 7 '09 #1
5 1287
donbock
2,426 Expert 2GB
@boddah
With the "%s" format code you promise printf that you will pass it the pointer to a null-terminated string. Instead, you pass it an integer value. It will treat that integer value as a pointer and try to peer into memory at that address -- most likely something bad will happen.

@boddah
Good -- you're using the "%d" format string in printf. However, you are violating the function prototype for memcpy. You should have gotten compiler errors. If you're not, then perhaps you're not including <string.h>.

What are you trying to accomplish with the memcpy?
Apr 7 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
memcpy?

Why not just:

Expand|Select|Wrap|Line Numbers
  1. temp[i] = 2* i;
?

BTW: in your snippet the variable a is not initialized. Plus, your loop does not fill the entire array.
Apr 7 '09 #3
boddah
15
char temp[20];
int a=0;
for(int i=0;i<10;i++)
{
printf("%d", 20 * i);
memcpy(temp[a], 2 * i, 1);
a++;
}
Hi again. Sorry I forgot to include the initialization of a=0 in the 2nd snippet. Actually the for loop will printf an output of something like this printf("%c%c", <some formula here>);
the for loop doesn't iterate at a fixed num so I'm just initializing the temp[20] for testing purposes. What I'm trying to achieve are; while the for loop outputs the printf("%c%c,<formula>) -> outputting 2 characters each time it loops, I wish to put everything in 1 variable or 1 array of character. Hope that fills you in. Thanks
Apr 8 '09 #4
newb16
687 512MB
Use sprintf to create a string with a representation of a number, and strcpy to append it to your array.
Apr 8 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
That's fine, but why the memcpy?

A char is an integer. Your i is an integer. The calculation produces an integer. You assign your integer result to an integer.

Just assign the result to your array element. The compiler does the math for you.

You can assign temp[i] and temp[i+1] on the same loop cycle if you are trying to make your array look like your printf output.
Apr 8 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
2
by: Florian Loitsch | last post by:
hi, What should be the output of the following code-snippet? === var x = "global"; function f() { var x = 0; eval("function x() { return false; }"); delete x; alert(x); }
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.